本文介绍了无法更改swagger ui路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Swagger / Swashbuckle version 5.6 to generate documentation for my ASP.Net Web API 2 project.

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

By default API documentation is accessible at URL http://localhost:56081/swagger/ui/index

但是,我希望它可以在 http://localhost:56081/apihelp/

But, I want it should be available at http://localhost:56081/apihelp/

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

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 ?

推荐答案

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

You can add the path to the call of EnableSwaggereUi,e.g.:

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

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

You can then call the UI with the URL http://localhost:56081/apihelp/index

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

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

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"));

重定向代码基于

这篇关于无法更改swagger ui路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:53