While working with areas I found that there was some occasional unexpected behavior with the order that routes get mapped.  Calling AreaRegistration.RegisterAllAreas() doesn’t give you any control over which area gets registered first.  If this becomes an issue, you may be so inclined to individually register routes.  Here is an approach:

public static void RegisterAreas(RouteCollection routes)

{

    routes.RegisterArea<BlogAreaRegistration>();

}

 

protected void Application_Start()

{

    //AreaRegistration.RegisterAllAreas();

    RegisterAreas(RouteTable.Routes);

    RegisterRoutes(RouteTable.Routes);

}


I created a RegisterAreas method just like RegisterRoutes.  Here, I can clearly register each Area one by one in the order that I choose.  This is done via an extension method on the RoutesCollection type.

public static class Extensions

{

    public static void RegisterArea<T>(this RouteCollection routes)

        where T : AreaRegistration

    {

        // instantiate the area registration

        AreaRegistration area = Activator.CreateInstance<T>();

 

        // create a context, which is just the name and routes collection

        AreaRegistrationContext context =

            new AreaRegistrationContext(area.AreaName, routes);

 

        // register it!

        area.RegisterArea(context);

    }

}


This extention method creates an instance of the AreaRegistration, and then creates an AreaRegistrationContext, and then calls RegisterArea on the area.

I find that this approach demystifies RegisterAllAreas and focuses on intention over convenience.  Registering areas individually is not always necessary, but it can be helpful to reduce routing confusion.

Wednesday, March 31, 2010 9:55:01 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1] -
Areas | ASP.NET MVC | Routing

John Nelson

mugshot I am a passionate C# Developer working in ASP.NET on an e-commerce solution for ticketing software. I work across all of the application layers, including server side functionality, and client side programming with jQuery and MS Ajax. Although my full time job is in WebForms, I spend many of my off hours working with MVC. I am especially interested in productivity and good programming practices.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
johncoder.com
Statistics
Total Posts: 41
This Year: 17
This Month: 0
This Week: 0
Comments: 4