Swashbuckle.WebApi: A route named 'swagger_docs' is already in the route collection. Route names must be unique. Parameter name: name

I have a fresh install of Swashbuckle installed. It ran successfully the first time with one API controller defined and then when I added a second controller, it puked this message.

[ArgumentException: A route named 'swagger_docs' is already in the route collection. Route names must be unique.
Parameter name: name]
   System.Web.Routing.RouteCollection.Add(String name, RouteBase item) +3746353
   System.Web.Http.WebHost.Routing.HostedHttpRouteCollection.Add(String name, IHttpRoute route) +107
   System.Web.Http.HttpRouteCollectionExtensions.MapHttpRoute(HttpRouteCollection routes, String name, String routeTemplate, Object defaults, Object constraints, HttpMessageHandler handler) +357
   Swashbuckle.Application.HttpConfigurationExtensions.EnableSwagger(HttpConfiguration httpConfig, String routeTemplate, Action`1 configure) +440
   Swashbuckle.Application.HttpConfigurationExtensions.EnableSwagger(HttpConfiguration httpConfig, Action`1 configure) +63

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 4
  • Comments: 30 (2 by maintainers)

Most upvoted comments

Delete bin/obj folders worked for me.

For me the fix was simple. SwaggerConfig contains this line at the start of file

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), “Register”)]

So it automatically gets called. But I was calling Register method on demand, the old-fashioned way. Removing manual call solved the case

I received this error when one project with Swagger referenced another project with Swagger. Removing the reference fixed the problem.

In my case, I had changed my project’s assembly name and default namespace and some old assemblies in the published bin folder caused the issue. I fixed it by checking the “Remove additional files at destination” setting in the Publish dialog, and then the site came right back up!

You may have two assembly which is loading swagger config at startup in your bin directory. I just had this error and this was because I renamed my project assembly without cleaning my bin. Fixed by deleting bin and obj

Out of the box, Swashbuckle uses WebActivitorEx to automatically hook into application startup via the following assembly directive in SwaggerConfig.cs

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

So, if you’re hosting in IIS (without OWIN), there should never be a need to manually invoke SwaggerConfig.Register as you’re doing above.

If all manual invocations are removed, you really shouldn’t get this error. It may be worth cleaning out all your bin folders again too as maybe you have some old DLL lying around.

Hey Guys! I had the same problem. Turns out the Register method method was being called twice. Once by the “PreApplicationStartMethod” Annotation and then again by my own code on the RouteConfig.

In my case a blank “SwaggerConfig.cs” got added to my project while I tried various versions of Swashbuckle.OData and Swashbuckle.Core packages. My config had been in WebApiConfig prior to this, and both were firing. It took me a bit to understand, because I did not know SwaggerConfig.cs had been added by some NuGet. The fix is to just use one of these - in my case move what I had in WebApiConfig to SwaggerConfig.

I had the following in my Global.asax Application_Start() method:

SwaggerConfig.Register(GlobalConfiguration.Configuration);
GlobalConfiguration.Configure(WebApiConfig.Register);

I commented out the first line and it started loading. That is really strange since this is the exact same code I am using in another web API project.

It is horrible - deleting the obj folder did it for me.

I deleted the obj and bin folders and rebuilt the solution. That solved the problem. I had binary files from another solution that I’d copied from before. Thanks!