21

I installed Swagger in my ASP.Net MVC Core project and it is documenting my API beautifully.

My co-worker asked me to install it in a full framework 4.6.1 project so I've done the following.

In Package Console Manager run:

Install-Package Swashbuckle

To get your Test Web API controller working: 1) Comment this out in the WebApi.config:

// config.SuppressDefaultHostAuthentication();
// config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

Now this URL: http://localhost:33515/api/Test brings back 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>

In Global.asax Register() I call:

 SwaggerConfig.Register();

In AppStart.Swagger.Config Register() I put:

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;

    }
}

Now I get this error: "A route named 'swagger_docsswagger/docs/{apiVersion}' is already in the route collection. Route names must be unique."

I've been stuck on this for hours. How do you get rid of this?

Sam
  • 4,766
  • 11
  • 50
  • 76

5 Answers5

42

This can happen when you re-name your .NET assembly. A DLL with the previous assembly name will be present in your bin folder. This causes the swagger error.

Delete your bin folder and re-build your solution. This resolves the swagger error.

Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
MichaelD
  • 8,377
  • 10
  • 42
  • 47
27

Swagger config uses pre-application start hook, so you don't need to call SwaggerConfig.Register() explicitly. Otherwise Register method is called twice.

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
opewix
  • 4,993
  • 1
  • 20
  • 42
  • I inherited code and one of my coworkers created the swaggerconfig.cs file. This is why I had two register lines. I commented out one config file and the run time error went away. – Ashley Feb 02 '21 at 22:05
2

in my case i added another project as refrence and that other project has swagger too. i remove that refrence and move needed code to new project.

hossein andarkhora
  • 740
  • 10
  • 23
1

I solved the problem by deleting the SwaggerConfig.cs file from the App_Start folder as I had already created it manually. Take a look at this link, here also has more useful information:

A route named 'DefaultApi' is already in the route collection error

César Augusto
  • 593
  • 1
  • 7
  • 7
0

In my experience the error occurs when you add reference to another project and that project is a service and it occurs on the SwaggerConfig of the referenced project. Removing project reference usually solve the problem, if you need to share classes I suggest you to create a specific project as Class Library and add its reference to both your services

Pietro Allievi
  • 386
  • 6
  • 14