Unable to change swagger ui path

醉酒当歌 提交于 2019-11-30 09:35:39

问题


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 ?


回答1:


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?



来源:https://stackoverflow.com/questions/45857289/unable-to-change-swagger-ui-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!