Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "A route named 'swagger_docs' is already in the route collection" after I publish my API App?

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?

like image 268
Panos Avatar asked Apr 03 '15 21:04

Panos


3 Answers

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.

like image 107
Panos Avatar answered Sep 25 '22 21:09

Panos


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.

like image 23
Venu Avatar answered Sep 23 '22 21:09

Venu


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.

like image 26
Sales Lopes Avatar answered Sep 25 '22 21:09

Sales Lopes