Add following in startup.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
In Swagger v2.0 with OpenAPi 2.0:
var basepath = "/api";
c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.BasePath = basepath);
c.PreSerializeFilters.Add((swaggerDoc, httpReq) => {
IDictionary<string, PathItem> paths = new Dictionary<string, PathItem>();
foreach (var path in swaggerDoc.Paths)
{
paths.Add(path.Key.Replace(basepath, "/"), path.Value);
}
swaggerDoc.Paths = paths;
});
In v5 with OpenApi v3.0:
var basePath = "/api";
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}" } };
});
});
References:
Posted On:
09-Mar-2021 03:34