locked
Error - An exception of type 'System.ArgumentException' occurred in System.Web.dll but was not handled in user code RRS feed

  • Question

  • User-797751191 posted

    Hi

    An exception of type 'System.ArgumentException' occurred in System.Web.dll but was not handled in user code

    Additional information: A route named 'DefaultApi' is already in the route collection. Route names must be unique.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;
    using System.Net.Http.Headers;
    
    
    namespace WebApi
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
    
                // Web API routes
                //config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
    
                config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            }
    
    
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    
    namespace WebApi
    {
        public class WebApiApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                WebApiConfig.Register(GlobalConfiguration.Configuration);
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                //RouteConfig.RegisterRoutes(RouteTable.Routes);
                //BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    }

    Thanks

    Friday, November 8, 2019 4:40 PM

All replies

  • User475983607 posted

    The error is very clear.   You have two Controllers/Actions with the same route name.   Can you share the relevant controller(s) and/or configuration?  Global.asax?

    Friday, November 8, 2019 5:36 PM
  • User-719153870 posted

    Hi jsshivalik,

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

    WebApiConfig.Register(GlobalConfiguration.Configuration);

    GlobalConfiguration.Configure(WebApiConfig.Register);

    It seems you called the Register twice that caused thie error. Please comment out one of them and uncomment the last three lines of code in your Global.asax.cs.

    Please refer to A route named 'DefaultApi' is already in the route collection and below code:

    WebApiConfig.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http.Headers;
    using System.Web.Http;
    
    namespace WebApiDemo01
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
    
                // Web API routes
                //config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
                config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            }
        }
    }

    Global.asax.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    
    namespace WebApiDemo01
    {
        public class WebApiApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                WebApiConfig.Register(GlobalConfiguration.Configuration);//you can comment this line out
    
                AreaRegistration.RegisterAllAreas();
                //GlobalConfiguration.Configure(WebApiConfig.Register);//Or this line
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    }

    Best Regard,

    Yang Shen

    Monday, November 11, 2019 3:15 AM