تولید API Documentation مدرن با Scalar در ASP.NET Core
نویسنده: محمد شریفی
تاریخ: ۱۴۰۴/۰۹/۱۸ ۰۹:۰۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
dotnet add package Scalar.AspNetCore
using Scalar.AspNetCore;
builder.Services.AddOpenApi();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}برای Swashbuckle.AspNetCore.SwaggerGen
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
if (app.Environment.IsDevelopment())
{
app.MapSwagger("/openapi/{documentName}.json");
app.MapScalarApiReference();
}builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument();
if (app.Environment.IsDevelopment())
{
app.UseOpenApi(options =>
{
options.Path = "/openapi/{documentName}.json";
});
app.MapScalarApiReference();
}builder.Services.SwaggerDocument();
if (app.Environment.IsDevelopment())
{
app.UseSwaggerGen(options =>
{
options.Path = "/openapi/{documentName}.json";
});
app.MapScalarApiReference();
}app.MapScalarApiReference();
app.MapScalarApiReference("/api-docs");
app.MapScalarApiReference("/docs");app.MapScalarApiReference(options =>
{
options.WithTitle("My API");
});app.MapScalarApiReference("/docs", options =>
{
options.WithTitle("My API Documentation");
});app.MapScalarApiReference((options, httpContext) =>
{
var isAdmin = httpContext.User.IsInRole("Admin");
options.WithTitle(isAdmin ? "Admin API" : "Public API");
});
// مسیر سفارشی با HttpContext
app.MapScalarApiReference("/docs", (options, httpContext) =>
{
options.WithTitle($"API برای {httpContext.User.Identity?.Name}");
});app.MapScalarApiReference(options =>
{
options.WithTitle("E-Commerce API")
.WithClassicLayout()
.ForceDarkMode()
.HideSearch()
.ShowOperationId()
.ExpandAllTags()
.SortTagsAlphabetically()
.SortOperationsByMethod()
.PreserveSchemaPropertyOrder()
.WithProxy("https://api-gateway.company.com")
.AddServer("https://api.company.com", "Production")
.AddServer("https://staging-api.company.com", "Staging");
});app.MapScalarApiReference(options =>
{
options.WithOpenApiRoutePattern("/api-spec/{documentName}.json");
});app.MapScalarApiReference(options =>
{
options.WithOpenApiRoutePattern("https://api.example.com/openapi/{documentName}.json");
});app.MapScalarApiReference(options => options.AddDocument("v1"));app.MapScalarApiReference(options => options.AddDocument("v1", "Production API"));app.MapScalarApiReference(options =>
{
options.AddDocument("v1", "Production API", "api/v1/openapi.json")
.AddDocument("v2-beta", "Beta API", "api/v2-beta/openapi.json", isDefault: true)
.AddDocument("internal", "Internal API", "internal/openapi.json");
});app.MapScalarApiReference(options => options
.AddPreferredSecuritySchemes("ApiKey")
.AddApiKeyAuthentication("ApiKey", apiKey =>
{
apiKey.Value = "sk-demo-key-12345";
}));app.MapScalarApiReference(options => options
.AddPreferredSecuritySchemes("BearerAuth")
.AddHttpAuthentication("BearerAuth", auth =>
{
auth.Token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...";
}));app.MapScalarApiReference(options => options
.AddPreferredSecuritySchemes("BasicAuth")
.AddHttpAuthentication("BasicAuth", auth =>
{
auth.Username = "demo-user";
auth.Password = "demo-password";
}));app.MapScalarApiReference(options => options
.AddPreferredSecuritySchemes("OAuth2")
.AddAuthorizationCodeFlow("OAuth2", flow =>
{
flow.ClientId = "scalar-demo-client";
flow.ClientSecret = "scalar-demo-secret";
flow.Pkce = Pkce.Sha256;
flow.SelectedScopes = ["read", "write", "admin"];
}));app.MapScalarApiReference(options =>
{
options.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient);
});app.MapScalarApiReference(options =>
{
options.WithBundleUrl("https://cdn.jsdelivr.net/npm/@scalar/api-reference");
});app.MapScalarApiReference(options =>
{
options.WithJavaScriptConfiguration("/scalar/config.js");
});builder.Services.Configure<ScalarOptions>(options => options.Title = "My API");
app.MapScalarApiReference().AllowAnonymous();