18

After publishing my API App I'm getting the yellow error screen of ASP.NET. The error message says "A route named 'swagger_docs' is already in the route collection".

How can I fix this?

Panos
  • 1,953
  • 1
  • 14
  • 15
  • 1
    In my case this happened after renaming the project and recompiling. The DLLs for the project with the priory project namespace was still in the BIN. Deleted everything from the bin and recompiled. – Elim Garak Sep 08 '17 at 23:22

6 Answers6

39

This is not related to API Apps per se but more around Web API. What triggers the error is pretty simple:

  1. You publish the API App which is based on Web API.
  2. You discard your project and start working on a new API App based on Web API
  3. You want to publish the new API App instead of the old API App you created at step 1.
  4. You select the API App during "Publish.." and you get the publishing profile of the existing API App we deployed at step 1.
  5. You deploy using Web Deploy and the publishing profile, the new API App on top of the old one.

That will trigger the issue I've explained before. That happens because there are two routes being registered by Swashbuckle when you try to start the app. One of the old one and one of the new one. That's because the old files are still present at the destination.

To solve this, during Web Deploy, click on the Settings tab and then expand the "File Publish Options". There is a checkbox there, called "Remove additional files from destination". This will fix the issue as it will only leave the files you deploy at the destination and not the old ones as well.

Settings -> File Publish Options

Hope it helps.

Panos
  • 1,953
  • 1
  • 14
  • 15
9

What if it happens when trying to debug the app locally ? This happened for me, and the reason was, I renamed my assembly name. So the bin folder had two dlls for the same project with different names which caused this error. Once I deleted the old named dll all is well. Hope this helps.

Venu
  • 109
  • 1
  • 4
  • This post isn't an actual attempt at answering the question. Please note [StackOverflow doesn't work like a discussion forum](http://stackoverflow.com/tour), it is a Q&A site where every post is either a question or an answer to a question. Posts can also have [comments](http://stackoverflow.com/help/privileges/comment) - small sentences like this one - that can be used to critique or request clarification from an author. This should be either a comment or a [new question](http://stackoverflow.com/questions/ask) – ρяσѕρєя K Jan 17 '17 at 07:41
8

This happens because You probally are configuring you route in your WebApiConfig class and SwaggerConfig class, as explained below:

WebApiConfig file:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        SwaggerConfig.Register();
    }
}

SwaggerConfig file:

using Swashbuckle.Application;

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

namespace NEOH.Api
{
    public class SwaggerConfig
    {
        public static void Register()
        {

What you should do is remove the assembly call on SwaggerConfig file.

It should work.

Sales Lopes
  • 131
  • 1
  • 5
  • Looks like some versions of the nuget package for Swagger automatically adds the SwaggerConfig file. Found this out while restoring an old API project and I had it showed up via nuget restore. Thanks! – ManOVision Oct 31 '22 at 16:16
2

My Solution & Cause:
I had the same problem when I renamed NamesSpaces,Refactored,etc.
After reading what everyone else did here's what I tried:

  • Cleaned the Solution in Visual Studio
  • Cleaned the Bin folder manually
  • Checked the nameSpace in the Project Properties (copied it just in case) >> Build tab >> Scrolldown to Output and ensure the XML documentation file is correct. You will need this name later.
  • Opened up: SwaggerConfig.cs >> fixed the name space in here (copy,paste) c.SingleApiVersion("vX","NameSpace")
  • Scrolled down until I found: GetXmlCommentsPath() copied and pasted the correct name space in the .xml file path.
  • Ran, smoke tested, finished this post.
Manuel Plaza
  • 170
  • 5
1

My issue was that I was referencing another project that had the Swashbuckle extension. Here is how I kept both projects without changing the anything in project that was referenced:

  1. Remove the routes created by the project referenced under SwaggerConfig.cs > Register right before GlobalConfiguration.Configuration.EnableSwagger(...).EnableSwaggerUi(...);:
// Clears the previous routes as this solution references another Swagger ASP.NET project which adds the swagger routes.
// Trying to add the Swagger routes more than once will prevent the application from starting
GlobalConfiguration.Configuration.Routes.Clear();
  1. Then, the application will be able to start, but you will see the operations/functions that are in both projects. To remove the operations from the project being referenced...

    1. Create the following class

      using Swashbuckle.Swagger;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Http.Description;
      
      namespace yournamespace.Models
      {
          /// <summary>
          /// This class allows to manage the Swagger document filters.
          /// </summary>
          public class SwaggerCustomOperationsFilter : IDocumentFilter
          {
              /// <summary>
              /// Applies the Swagger operation filter to exclude the Swagger operations/functions 
              /// that are inherited by the other Swagger projects referenced.
              /// </summary>
              /// 
              /// <param name="p_swaggerDoc">Swagger document</param>
              /// <param name="p_schemaRegistry">Swagger schema registry</param>
              /// <param name="p_apiExplorer">Api description collection</param>
              public void Apply(SwaggerDocument p_swaggerDoc, SchemaRegistry p_schemaRegistry, IApiExplorer p_apiExplorer)
              {
                  IEnumerable<ApiDescription> externalApiDescriptions = p_apiExplorer.ApiDescriptions
                      .Where(d => d.ActionDescriptor.ControllerDescriptor.ControllerType.Module.Name != GetType().Module.Name);
      
                  IEnumerable<int> externalApiDescriptionIndexes = externalApiDescriptions
                      .Select(d => p_apiExplorer.ApiDescriptions.IndexOf(d))
                      .OrderByDescending(i => i);
      
                  IEnumerable<string> externalPaths = externalApiDescriptions.Select(d => $"/{d.RelativePathSansQueryString()}");
      
                  foreach (string path in externalPaths)
                  {
                      p_swaggerDoc.paths.Remove(path);
                  }
      
                  foreach (int apiDescriptionIndex in externalApiDescriptionIndexes)
                  {
                      p_apiExplorer.ApiDescriptions.RemoveAt(apiDescriptionIndex);
                  }
              }
          }
      }
      
    2. And add the following in SwaggerConfig.cs > Register > GlobalConfiguration.Configuration.EnableSwagger(...)
      c.DocumentFilter<SwaggerCustomOperationsFilter>();
      
0

Alternative cause of this problem:

Seems like a lot of people have this issue resolved by deleting their "bin" and "obj" folders as per the other answers.

However the cause of the issue might be that you are configuring your Swagger Config in a referenced project, as per this comment: https://github.com/domaindrivendev/Swashbuckle/issues/364#issuecomment-226013593

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

This caused me to split some core functionality out into a Third project that both of my API's could reference, rather than them referencing each other.

Morvael
  • 3,478
  • 3
  • 36
  • 53