我正在将.NET Core应用程序从Swashbuckle v4升级到v5。我目前在Startup.ConfigureServices中具有以下代码,用于定义相对路径:
app.UseSwagger(c => {
c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.BasePath = "/folder/data");
});
我无法将其转换为v5。我尝试了以下操作,但看不到在哪里设置路径:
c.PreSerializeFilters.Add((swagger, httpReq) =>
{
var paths = new OpenApiPaths();
var pathItem = new OpenApiPathItem();
paths.Add("main", pathItem);
swagger.Paths = paths;
});
答案 0 :(得分:1)
在v5中,basePath被服务器替换,根据: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1173
您可以使用routeTemplate设置json文件的路径,以及使用服务器从ui发出的请求的路径:
var basePath = "/v1";
app.UseSwagger(c =>
{
c.RouteTemplate = "swagger/{documentName}/swagger.json";
c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
{
swaggerDoc.Servers = new List<OpenApiServer> { new OpenApiServer
{ Url = $"{httpReq.Scheme}://{httpReq.Host.Value}{basePath}" } };
});
});
并配置ui:
app.UseSwaggerUi(options =>
{
options.swaggerEndpoint($"swagger/{apiVersion}/swagger.json, "my api v1");
}
相对路径可以通过./或../
用于端点