是什么导致了swagger已经在Web API 2的路由集合中出现的错误?

Sam*_*Sam 9 swagger asp.net-web-api2 swashbuckle

我在我的ASP.Net MVC Core项目中安装了Swagger,它正在精美地记录我的API.

我的同事让我在一个完整的框架4.6.1项目中安装它,所以我做了以下工作.

在Package Console Manager中运行:

Install-Package Swashbuckle
Run Code Online (Sandbox Code Playgroud)

要使Test Web API控制器正常工作:1)在WebApi.config中注释掉它:

// config.SuppressDefaultHostAuthentication();
// config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
Run Code Online (Sandbox Code Playgroud)

现在这个URL: http:// localhost:33515/api/Test 带回XML:

<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>value1</string>
<string>value2</string>
</ArrayOfstring>
Run Code Online (Sandbox Code Playgroud)

在Global.asax Register()中,我调用:

 SwaggerConfig.Register();
Run Code Online (Sandbox Code Playgroud)

在AppStart.Swagger.Config Register()中我放了:

public class SwaggerConfig
{
    public static void Register()
    {     
       var thisAssembly = typeof(SwaggerConfig).Assembly;
       GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1.0", "HRSA CHAFS");
                    c.IncludeXmlComments(GetXmlCommentsPath());
                })
              .EnableSwaggerUi(c =>
                {});
    }

    private static string GetXmlCommentsPath()
    {
        var path = String.Format(@"{0}bin\Services.XML", AppDomain.CurrentDomain.BaseDirectory);
        return path;

    }
}
Run Code Online (Sandbox Code Playgroud)

现在我收到这个错误:"路由集合中已经有一条名为'swagger_docsswagger/docs/{apiVersion}'的路由.路由名称必须是唯一的."

我已经坚持了几个小时.你怎么摆脱这个?

Mic*_*elD 20

重命名.NET程序集时可能会发生这种情况.具有先前程序集名称的DLL将出现在bin文件夹中.这会导致招摇错误.

删除bin文件夹并重新构建解决方案.这解决了招摇错误.


ope*_*wix 17

Swagger 配置使用应用程序前启动钩子,因此您不需要SwaggerConfig.Register()显式调用。否则Register方法被调用两次。

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
Run Code Online (Sandbox Code Playgroud)