简体   繁体   中英

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 ?

You can add the path to the call of EnableSwaggereUi , eg:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM