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

Total Post:412

Points:2888
 4330  View(s)
Ratings:
Rate this:

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

  1. Post:194

    Points:1396
    Re: A route named 'DefaultApi' is already in the route collection

    Instead of adding it in the route collection, added it in HttpConfiguration

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

      Modified On Apr-07-2018 01:50:31 AM

Answer