无法更改 swagger ui 路径

Unable to change swagger ui path

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

默认情况下 API 文档可在 URL http://localhost:56081/swagger/ui/index

访问

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

可用

我搜索了很多,尝试更改 SwaggerConfig.cs 文件中的设置,但似乎没有任何效果。

那么,这可能吗?如果是,谁能帮我解决这个问题?

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

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

然后您可以用 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"));

重定向代码基于 v.karbovnichy 在

中的回答