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.