9

This question may seems duplicate but this is slightly different. In all other question in SO I had noticed that they have multiple routes registered. but in my case I have just one route.

I am creating asp.net webapi (framework 4.5) and have just one route in RegisterRoutes() method -

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "DefaultApi",
            url: "rest/{controller}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );

    }

Then why is it throwing error?

A route named 'DefaultApi' is already in the route collection. Route names must be unique. Parameter name: name
Kara
  • 6,115
  • 16
  • 50
  • 57
Anil Purswani
  • 1,857
  • 6
  • 35
  • 63
  • 1
    You added the tag *web-api*. When you use WebApi you add your routes to an instance of `HttpConfiguration`, not `RouteCollection`. Also you use `MapHttpRoute()`. – user3038092 Dec 05 '13 at 07:25

5 Answers5

24

I had a similar issue with adding a route DefaultApi. Though the exact 'additional details' message in my ArgumentException stack trace was:

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

I ofcourse made sure I was adding the DefaultApi route only once, but in the end noticed that in Global.asax's Application_Start method I was calling the WebApiConfig.Register(..) twice, though in the following - not immediately obvious - way:

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

Another serious case of 'copypasterites'! I simply removed the WebApiConfig.Register(..) line and that fixed my issue.

(I am using WEB API 2.0/.NET 5)

Bart
  • 5,065
  • 1
  • 35
  • 43
  • In all the land this is the only place where I found the solution to my problem, buried as a 0 post in some related question. Good job, thanks! Up and down for hours, never spotted it. – Marcelo Mason May 22 '14 at 10:25
9

CAUSE: renaming namespaces without removing associated bin and output files.

SOLUTION: manually delete the bin and obj folders from the output directory. (cleaning the solution is not enough, some problematic residual files remain causing this problem.)

... that was my experience anyway.

3

Fine, I resolved it based on the reply by user3038092. Instead of adding it in the route collection, I added it in HttpConfiguration

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "rest/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

And it worked.

Anil Purswani
  • 1,857
  • 6
  • 35
  • 63
1

If you are using MVC4 or MVC5 application. Then put your Route Configuration in

WebApiConfig.cs and

also check route name should be unique in both files i.e RouteConfig.cs and WebApiConfig.cs

Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
0

I see that you call the controller but you do not give the controller name like this:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "DefaultApi",
            url: "rest/{controller}/{id}",
            defaults: new { controller="Home",action="Index",id = UrlParameter.Optional }
        );

    }

that means you regist HomeContrller for the Index view,

trai bui
  • 588
  • 12
  • 36
  • No, that does not mean you register the HomeController for web. Controllers do not need to be explicit registered. The **default** argument is for **default** values, when a route is missing that particular argument. – user3038092 Dec 05 '13 at 07:17
  • do you have use Areas in ASP.net ? if not, it can not have more than one routest – trai bui Dec 05 '13 at 07:20
  • @traibui, I don't have home controller and not even areas in my project. moreover, even if it is related to the controllername, the error I mentioned surely doesn't match with the solution you suggested. but anyways thanks for your suggestion somewhere it may help to someone else – Anil Purswani Dec 06 '13 at 06:52