Asp.net MVC Areas in depth
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); 1
RegisterRoutes(RouteTable.Routes);
}
In the Application_Start event, the Asp.net MVC application registers the areas by calling a static function on the AreaRegistration abstract class 1.
public static void RegisterAllAreas()
{
RegisterAllAreas(null);
}
public static void RegisterAllAreas(object state)
{
2 3
RegisterAllAreas(RouteTable.Routes, new BuildManagerWrapper(), state);
}
RegisterAllAreas function was finally invoked with a RouteCollection 2 and an IBuildManager instance 3.
Buildmanager provides the methods to manage the Asp.net compilation. GetReferencedAssemblies method of the Buildmanager will return a collection of assemblies which includes those specified in assemblies element of web.config, assemblies built from App_Code folder, and assemblies from other top level folders.
internal static void RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, object state)
{
4
List areaRegistrationTypes = TypeHelpers.FilterTypesInAssemblies(buildManager, IsAreaRegistrationType);
foreach (Type areaRegistrationType in areaRegistrationTypes)
{
AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(areaRegistrationType);5
registration.CreateContextAndRegister(routes, state);
}
}
From the referenced assemblies, the application filters, using a predicate 4, those of AreaRegistration Type and creates an instance for each item found 5. In asp.net MVC preview 2 a class that is derived from AreaRegistration Type should be present in the corresponding area's folder and the instances of these classes are the ones created in the above step.
public class Routes: AreaRegistration 6 { 7 public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute("blog_default", "Blog/{controller}/{action}/{id}", new {controller = "Post", action = "index", id = ""});8 } public override string AreaName 5 { get { return "blog"; } } }
CreateContextAndRegister method of AreaRegistration instance packages the RouteCollection, object state, and area name 5 (which is obtained from the user defined AreaRegistration derived class 6) in to an AreaRegistrationContext and passes the context 7 to the RegisterArea method which is implemented in the user defined AreaRegistration class. The custom defined AreaRegistration class then can add the required routes 8 to the RoutesCollection and the internals of these will be further explored in the next post.
2 Comments
Vimal said
October 05, 2009
Good Article
Kiran said
October 05, 2009
Wonderful