A route named 'DefaultRoute' is already in the route collection. Route names must be unique

17,293

Solution 1

I came to similar problem with already set routes. For me the solution was here: https://stackoverflow.com/a/28503674/1496138 . Problem was I later renamed project file - old DLL files with previous name still existed in the /bin directory and automatic startup configuration run them too.

Solution 2

Routes are usually configured one of three places. The global.asax file, the RouteConfig.cs file (usually found in /App_Start or in an {areaname}AreaRegistration.cs file which is found in /Areas/{areaname}. There must be a duplicate route name in these places. Since it is giving you the name that is duplicated, try searching for "DefaultRoute" in your project and it will probably jump out. Change the name of one of them and it should fix your problem.

Solution 3

I got this error when I had code that explicitly setting route names using a RouteAttribute, when I was trying to set up a route that would take different arguments depending on whether it was called with HTTP GET or HTTP POST:

[HttpGet]
[Route("MyApiMethod", Name = "MyApiMethod")]
public MyApiMethod PropertyData(...)
{
    ...
}

// ERROR: Causes "System.ArgumentException: A route named 'DefaultRoute' is already in the route collection. Route names must be unique."
[HttpPost]
[Route("MyApiMethod", Name = "MyApiMethod")]
public MyApiMethod PropertyData(...)
{
    ...
}

This wasn't immediately obvious to me, but the problem turned out to be exactly what the error message says: I had two routes with the same Name value. Just changing those Name values to be unique (without needing top change the route itself or the method signatures) fixed the issue:

[HttpGet]
[Route("MyApiMethod", Name = "MyApiMethod-GET")]
public MyApiMethod PropertyData(...)
{
    ...
}

[HttpPost]
[Route("MyApiMethod", Name = "MyApiMethod-POST")]
public MyApiMethod PropertyData(...)
{
    ...
}

Solution 4

Cleaning the solution/project didn't work for me. I ended up navigating to the bin directory and deleting all the files in the directory because it was an absolute mess and that resolved the problem for me.

Share:
17,293
xaisoft
Author by

xaisoft

It's just a game.

Updated on July 14, 2022

Comments

  • xaisoft
    xaisoft almost 2 years

    When I publish an ASP.NET WebAPI solution to a remote IIS Server, I get the error message:

    Message: System.ArgumentException: A route named 'DefaultRoute' is already in the route collection. Route names must be unique.

    I saw this thread with the same problem, but nothing on it has worked. I have tried:

    1. Deleting all bin/obj folders in all projects.
    2. Cleaning/Rebuilding
    3. Deleting files off the the remote server before publishing
    4. Renaming the project

    Is there anyway I can find out if there is a stale file. I did rename some files and I heard that this can cause a problem?

    Not sure if this matters, but I am using the ASP.NET WebApi along with RestSharp to make my rest calls.

    This is what my Global.asax startup has: Is it redundant?

    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RouteConfig.RegisterRoutes(RouteTable.Routes);