How to Use Areas in ASP.NET MVC

This article describe about Areas in ASP.NET MVC application and organize application structure in better and simple way.

A large enterprise application is consist of many inter connected sub-applications. For such application organizing individual module is tedious task. The Area of MVC helps you to make it simple. And write more maintainable code.


Area introduced in ASP.Net MVC2. You have to configure Areas as part of Routing. You can check more about Routing examples. You can add model, views and controller to Area of specific functional part of application eg. Orders, Customers, Products and so on...

Lets say you are working on one e Commerce project where you have different functional parts as Order, Customer, Products and so on. So you will create Areas for each functional part as shown in below picture.

ASP.NET MVC Area


Creating Area:

For adding new Area, right click on project file from Solution Explorer -> select Add -> select Area.

“Area Name” dialog box appears, give Area name of your choice. I am giving it as “Orders”. Click Ok.

It adds folder Model, Controllers and Views with Shared View folder. It also add web.config file specific to this Area.

It also add special file named as “<areaname>AreaRegistration”. In my case it is OrdersAreaRegistration. This file is inherited by AreaRegistration. This file has RegisterArea method, which gets called from Application_Start event. It add routes to context object specific to this Area.

You can see below code in OrdersAreaRegistration.


public class OrdersAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Orders";
        }
    }

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

Register Area

Every Area has AreaRegistraion.cs file containing RegisterArea method. It should get called by Application_Start event. Otherwise the routing to Area will not work.

Application_Start event has AreaRegistration call as AreaRegistration.RegisterAllAreas().

You can see below code in Global.asax file


protected void Application_Start()
{
        //Register all application Areas
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);  
}    
    
    

RegisterAllAreas should be called before other configuration so that other settings, routing, filters, bundles can be applied on Areas as well. Get more details about ASP.NET MVC filters.

Routing Engine will check all registered Areas and process browser's request. See more on ASP.NET MVC Request life cycle.


Using Area

Add new Controller by right click on Controllers folder under Orders Area. Select Add -> Controller name it as Order and click Ok.

Open OrderController -> right click on Index method and select Add View. Name this View as Index and click Ok.

Run your application and browse below URL

http://localhost:<port number>/orders/order

You will see Index view from Orders View

ASP.NET MVC Area Example

Download source code

Speak your mind :
Leave a comment for this article on dotnetbloogers.com