2

Using MVC3/4 if you have area's in your solution, what is the order that it will try and resolve the areas and root level controllers?

For example:

Does it first try the root level routes and then the area level routes in alphabetical order?

Or does it first check the area level routes in alphabetical order and then the root level?

Thanks

shenku
  • 9,802
  • 10
  • 57
  • 109

1 Answers1

1

MVC resolvers area specific routes first and then root level routes. This is because by default you have next code in Global.asax:

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas(); //1. registers areas

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes); //2. only after that register root routes
    }

I have not tried it but if you would like to change this behavior you may try to swap these code lines in you project.

petro.sidlovskyy
  • 4,889
  • 1
  • 23
  • 28
  • 1
    and what about the order of the area registration? – shenku Sep 06 '12 at 06:15
  • 1
    I have just review went through area registration code and haven't found any code which is order specific. Also in this article http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/ is mentioned: "Took me hours to figure this out. That MVC cannot guarantee the order of which Area’s are registered." You may google for a solution how to register areas in specific order (i have seen it) but usually areas should be so much independent so you shouldn't take care for their order. This is why they were created. – petro.sidlovskyy Sep 06 '12 at 06:32
  • the order they resolve in surely would matter though? that is why you do more specific routes to least specific routes, right? (in which case I care the order they resolve in) – shenku Sep 06 '12 at 06:37
  • Yes you are right, but usually each area route starts with area name or area name is somewhere in the middle of the route. So there should not be collisions between similar routes from different areas. – petro.sidlovskyy Sep 06 '12 at 06:42
  • Few more links you may want to read: http://stackoverflow.com/questions/1639971/mvc-2-arearegistration-routes-order http://forums.asp.net/t/1642939.aspx/1?Tips+Area+registration+order+here+solution+ – petro.sidlovskyy Sep 06 '12 at 06:44