.Net Core / .Net 6 项目更改Swagger 路由:
定义一个路由前缀或从配置里读取
var routePrefix = AppSettingsExt.Get("route_prefix");
xml文档按常规方式引入
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = docTitle });
var files = new DirectoryInfo(AppContext.BaseDirectory).GetFiles("*.xml", SearchOption.AllDirectories);
foreach (var file in files)
{
c.IncludeXmlComments(file.FullName);
}
});
更改路由配置,UseSwagger 以及UseSwaggerUI 都需要修改
app.UseSwagger(c =>
{
c.RouteTemplate = routePrefix + "/swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/{routePrefix}/swagger/v1/swagger.json", docTitle);
c.RoutePrefix = $"{routePrefix}/swagger";
});