无法改变swagger ui路径

Raj*_*aik 2 swagger-ui asp.net-web-api2 swashbuckle

我正在使用Swagger/Swashbuckle 5.6版为我的ASP.Net Web API 2项目生成文档.

默认情况下,可以通过URL http:// localhost:56081/swagger/ui/index访问API文档

但是,我希望它应该在http:// localhost:56081/apihelp /

我搜索了很多,尝试更改SwaggerConfig.cs文件中的设置,但似乎没有任何东西使这个工作.

那么,这甚至可能吗?如果是的话,任何人都可以帮助我吗?

jps*_*jps 8

您可以添加调用的路径EnableSwaggereUi,例如:

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

然后,您可以使用URL http:// localhost:56081/apihelp/index调用UI

请参阅https://github.com/domaindrivendev/Swashbuckle#custom-routes以供参考.

请注意:默认设置'swagger'会自动重定向到'swagger/ui/index',但是当您使用'apihelp'时,此自定义设置不会自动重定向到'apihelp/index'.要实现自动重定向,您可以在WebApiConfig以下位置添加路线:

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

重定向代码基于v.karbovnichy在如何从根URL重定向到/ swagger/ui/index的答案