A swagger-ui dist package for aspnetcore. Let the aspnetcore program provide swagger-ui endpoint.
$ dotnet add package SwaggerUI.AspNetCoreA swagger-ui dist package for aspnetcore. Let the aspnetcore program provide swagger-ui endpoint.
Microsoft.AspNetCore.OpenApiInstall package
dotnet add package SwaggerUI.AspNetCore
Setup with code
Add MapSwaggerUI into request pipeline.
app.MapSwaggerUI();
Now it will work with Microsoft.AspNetCore.OpenApi (aspnetcore api template version of .net9 or higher). Now access /swagger, you will see swagger-ui.
app.MapSwaggerUI(routePrefix: "/swagger",
openApiEndpoint: "/openapi/v1.json");
routePrefix set the swagger-ui access pathopenApiEndpoint set the openapi doc access path
aspnetcore api template in .net9 default is /openapi/v1.jsonrelative path or absolute path
swagger. Normally, the path is /swagger/v1/swagger.jsonabsolute path, CORS is requiredapp.MapSwaggerUI(options =>
{
options.RoutePrefix = "/swagger";
//support named and multi openapi endpoint
options.OpenApiEndpoints.Add("/openapi/v1.json");
//Javascript code run before swagger-ui initialization
options.CustomCodeBeforeInitialization =
"""
alert("Hello");
""";
//Javascript object to set and override all configuration before
options.CustomConfigurationObject =
"""
{
url: null,
urls: [ { url: "/openapi/v1.json", name: "default"}]
}
""";
});
Support full swagger-ui configuration by set Javascript object code for CustomConfigurationObject
More configuration references: swagger-ui configuration doc