Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

asp.net web api2 - Unable to change swagger ui path

I'm using Swagger / Swashbuckle version 5.6 to generate documentation for my ASP.Net Web API 2 project.

By default API documentation is accessible at URL http://localhost:56081/swagger/ui/index

But, I want it should be available at http://localhost:56081/apihelp/

I searched a lot, tried changing settings in the SwaggerConfig.cs file but nothing seems to make this work.

So, is this even possible? if yes, can anyone help me with this ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can add the path to the call of EnableSwaggereUi, e.g.:

SwaggerConfig.Register(HttpConfiguration config)
{
    config.EnableSwaggerUi("apihelp/{*assetPath}", c => 
    ... // following lines omitted
}

You can then call the UI with the URL http://localhost:56081/apihelp/index

See https://github.com/domaindrivendev/Swashbuckle#custom-routes for reference.

Please note: the default setting 'swagger' redirects automatically to 'swagger/ui/index', but this custom setting does not automaically redirect to 'apihelp/index' when you just use 'apihelp'. To achieve an automatic redirect you can add the route in WebApiConfig:

config.Routes.MapHttpRoute(
    name: "Swagger UI",
    routeTemplate: "apihelp",
    defaults: null,
    constraints: null,
    handler: new RedirectHandler(message => message.RequestUri.ToString().TrimEnd('/'), "/index"));

The redirect code is based on v.karbovnichy's answer in How to redirect from root url to /swagger/ui/index?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...