A route named “x” is already in the route collection. Route names must be unique. Exception with ASP.NET MVC 3

后端 未结 16 1909
难免孤独
难免孤独 2020-12-02 16:24

I\'m doing an ASP.NET MVC 3 web service and I keep getting this exception intermittently.

Stack trace:

Server Error in \'/\' Application.

A route n         


        
相关标签:
16条回答
  • 2020-12-02 17:01

    The routes get loaded from all assemblies within AppDomain.CurrentDomain, so if your old assemblies are still part of that, you might be still getting old/duplicate routes.

    0 讨论(0)
  • 2020-12-02 17:03

    I was running an old MVC2 website and I got this issue because the IIS 'Managed Pipeline Mode' was set on 'Integrated' by default (press F4 on the project). Changing it to 'Classic' fixed the issue

    0 讨论(0)
  • 2020-12-02 17:04

    This error can occur due to multiple causes, I had the same error and solved it by modifying the Global.asax class.

    The Application_Start method at Global.asax.cs was like:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
    

    The following line occurs twice in this method:

    RouteConfig.RegisterRoutes(RouteTable.Routes);
    

    This ensured that the route was twice added to the route list and at the same time causing the error.

    I changed the Application_Start method as follows and the error disappeared:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
    

    This may not be the answer for your problem, but can perhaps help others in the future. I didn't see this answer between the others, so I decided to add this.

    0 讨论(0)
  • 2020-12-02 17:05

    I had an application that was a Forms app migrated to MVC with a third party component used for authentication which redirected to another site. The component would start a session twice if the user wasn't already logged in (once for initial connection to site and once for return). So I solved this with the following code:

    if (routes.Count < 3)
                {
                    routes.IgnoreRoute("login.aspx");
                    routes.IgnoreRoute("default.aspx");
                    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                    routes.MapRoute(
                        name: "Default",
                        url: "{controller}/{action}/{id}",
                        defaults: new {action = "Index", id = UrlParameter.Optional}
                        );
                }

    0 讨论(0)
提交回复
热议问题