Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A route named 'DefaultApi' is already in the route collection

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
like image 760
Anil Purswani Avatar asked Dec 05 '13 06:12

Anil Purswani


2 Answers

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)

like image 93
Bart Avatar answered Sep 28 '22 06:09

Bart


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.

like image 22
Simon Porcella Avatar answered Sep 28 '22 07:09

Simon Porcella