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
I tried deleting all files in the bin and obj folders, cleaning the solution, and rebuilding with no success. I am using Local IIS (not express).
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.
The hack fix no longer works. Any ideas on how to flush this error?
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.
I followed the instructions provided from #223 to setup so it runs through IIS. I removed the assembly attribute from the AssemblyInfo file and the only invocation I have is the one in Global.asax as shown above.
Interestingly it doesn't appear my local IIS instance is picking up changes I make to the SwaggerConfig file. I removed the var thisAssembly = typeof(SwaggerConfig).Assembly; line from the file, cleaned solution, deleted ASP.NET Temporary Files, and rebuilt the solution but that line still shows up as shown in the image below:
Delete bin/obj folders worked for me.
I'm going to close this one as I can only conclude that it's due to some oddity with your deployment (e.g. stale dlls). There is no code in the SB repo that would automatically register the route twice
I did all the bin and obj clean and still get the same error.
I am getting the same error. Deleting bin and obj did not work for me.
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
I had two assemblies with SwaggerConfig. Don't know how the other assembly got a copy (probably slip of the mouse). Deleting the errant copy 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!
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!
I received this error when one project with Swagger referenced another project with Swagger. Removing the reference fixed the problem.
I had the same as ericsundquist but did not read to the bottom of this page ;(
1h of my life I'll never get back..
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.
If anybody is still having this issue: commenting out the below in SwaggerConfig.cs helped me, and swagger still works nevertheless:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
... and so on until the end of the method
It is horrible - deleting the obj folder did it 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
Got this error after renaming my Assembly name; Deleting both bin and obj worked for me.
none of this options worked for me =/
I also had the same issue as ericsundquist. Check your list of referenced projects. I had accidentally referenced a project that was also using swagger. The way to tell was to remove any use of "GlobalConfiguration.Configuration.EnableSwagger" in the project with this error, then navigate to the default swagger url http://localhost:[PORT_NUM]/swagger/docs/v1. At that url I found a full swagger response of all the API endpoints in my project but named as the referenced project. Once I removed the reference to the other project, everything started working as expected.
SOLUTION DESCRIPTION:
There may be residual files on the server side. Reset the deployment publishing profile to "Remove additional files at destination" then republish.
SOLUTION PROCEDURE:
In the Visual Studio PUBLISH dialog box:
So we're reasonably sure that the reason we're getting this is that one of our three projects with Swagger references another one. It has for quite some time, ever since we introduced Swagger. To begin with, everything worked. But after yet another reference between the two projects, this problem has appeared.
Any ideas of how to amend this before we get the two projects entirely separated? Like when we do
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
Could we possibly prepend that with some sort of check to see if the route is already registered?
So, in development, my Web API project references Swashbuckle with it's own NuGet package file. Everything works fine.
Then, one day I want to take some of my specialized classes for customizing the Swashbuckle output, and put them into a more generic utility class..
So, I create a new class library and to add to it a new NuGet/Swashbuckle reference to that generic library.. that's when I inadvertantly introduce the add'l SwaggerConfig that others have already commented on above.
@estomagordo - I'm assuming your scenario is like the one I've detailed in my prior comment...
I can imagine if you delete the App_Start/SwaggerConfig file that gets added to a "utility library" (see my scenario above).. it could get re-introduced later .. when you do a fresh checkout from the code repository.. and run the NuGet commands anew on virgin disk space .. it could re-generate the App_Start/SwaggerConfig file in a project you previously deleted it from.
Thus, it may be better to comment it out as others have noted above.
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.
delete all bin on the server,and realease again.
my issue was we had a base project on all our projects.. and renamed the namespace. Deleting the bin folder gets rid of the old dlls and fixes this error
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:
Most helpful comment
Delete bin/obj folders worked for me.