[ASP.NET MVC] Routing – Areas

Areas are another way to organize an MVC application, especaily when it becomes large. An application can be divided into multiple functional segments, or  areas.

1. Areas

An area is like a mini-MVC application. It has its own controllers, models, views, and configurations. Suppose you create an online store application and wants to add pages for administrators. Wouldn’t it be nice if you can isolate admin functionalities in one place?

2. Creating an Area

  • Right-click the project item in the Solution Explorer
  • Select Add – Area…
  • On the “Add Area” dialog, type the name for the area you are adding

Visual Studio automatically configures the project for a new area.

The new “Areas” folder is created in the root. In it, you can see the new folder with the name you provided. Now you can see the mini-MVC structure in the folder.

3. AreaRegistration Class

System.Web.Mvc.AreaRegistration” is an abstract class that is used to register an area in an ASP.NET MVC application.

When you add a new area, VS automatically adds a new class “<AreaName>AreaRegistration” class derived from “AreaRegistration“. Also one property “AreaName” and a method “RegisterArea” are overriden.

public class AdminAreaRegistration : AreaRegistration
{
  public override string AreaName
  {
    get
    {
      return "Admin";
    }
  }

  public override void RegisterArea(AreaRegistrationContext context)
  {
    context.MapRoute(
      "Admin_default",
      "Admin/{controller}/{action}/{id}",
      new { action = "Index", id = UrlParameter.Optional }
    );
  }
}

If you are not satisified the route configuration, you can freely modify the “RegisterArea” function.

4. Global.asax

How MVC application recognized areas then? Areas are logical segements and do not differ from others. The centrally registered routes will redirect requests to appropriate controllers in different areas. So routes for areas should be registered in “Global.asax”

protected void Application_Start()
{
  AreaRegistration.RegisterAllAreas();
  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);
}

You can find the “Application_Start()” method in the “Global.asax.cs

System.Web.Mvc.AreaRegistration” has a static method “RegisterAllAreas()”. When this method is called, MVC Framework finds all classes derived from “AreaRegistration” and calls the “RegisterArea” method on each of them. By doing so, all routes for areas are registered.

  • public static void RegisterAllAreas()

 

 

3 thoughts on “[ASP.NET MVC] Routing – Areas

  1. Pingback: MVC Areas in Sitecore | Yet Another Developer Blog

  2. Pingback: MVC Areas in Sitecore | Developer Tales

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s