What is causing the error that swagger is already in the route collection for Web API 2?
Solution 1
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.
Solution 2
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")]
Solution 3
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.
Solution 4
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
Related videos on Youtube
Sam
Updated on November 14, 2022Comments
-
Sam over 1 year
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?
-
MichaelD over 6 yearsDid you find a solution?
-
Arunprasanth K V over 5 yearsclean solution delete all bin/release folders and try to build again
-
-
Abhas Bhoi over 3 yearsThanks. Worked for me
-
Ashley about 3 yearsI 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.