A route named 'DefaultApi' is already in the route collection A route named 'DefaultApi' is already in the route collection asp.net asp.net

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


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)


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.


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.