简体   繁体   English

无法更改swagger ui路径

[英]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. 我正在使用Swagger / Swashbuckle 5.6版为ASP.Net Web API 2项目生成文档。

By default API documentation is accessible at URL http://localhost:56081/swagger/ui/index 默认情况下,可以通过URL http:// localhost:56081 / swagger / ui / index访问API文档。

But, I want it should be available at http://localhost:56081/apihelp/ 但是,我希望它应该在http:// localhost:56081 / apihelp /中可用

I searched a lot, tried changing settings in the SwaggerConfig.cs file but nothing seems to make this work. 我进行了大量搜索,尝试更改SwaggerConfig.cs文件中的设置,但似乎没有任何效果。

So, is this even possible? 那么,这可能吗? if yes, can anyone help me with this ? 如果是,有人可以帮助我吗?

You can add the path to the call of EnableSwaggereUi , eg: 您可以将路径添加到EnableSwaggereUi的调用,例如:

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 然后,您可以使用URL http:// localhost:56081 / apihelp / index调用UI

See https://github.com/domaindrivendev/Swashbuckle#custom-routes for reference. 请参阅https://github.com/domaindrivendev/Swashbuckle#custom-routes以获取参考。

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'. 请注意:默认设置'swagger'自动重定向到'swagger / ui / index',但是当您仅使用'apihelp'时,此自定义设置不会自动重定向到'apihelp / index'。 To achieve an automatic redirect you can add the route in WebApiConfig : 要实现自动重定向,您可以在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? 重定向代码基于v.karbovnichy在“ 如何从根url重定向到/ swagger / ui / index?”中的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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