w3resource

Areas in ASP.NET MVC


In the past tutorials, throughout these series, we have looked at a whole lot of concepts, found in an ASP.NET MVC application.

You may have wondered, what happens in a big application where we will have different sections like Admin, Sales, Accountants where each person is meant to see a different view with a totally new background.

Large application as described above will include a large number of controllers, views and model classes. hence maintaining large number of views, models and controllers with the default ASP.NET MVC project structure can become tedious

Since maintenance of such application is very paramount, the need for a clean organization cannot be over emphasized thus ASP.NET MVC Areas.

Areas were introduced in ASP.NET MVC 2. Area allows us to partition large application into smaller units where each unit contains separate MVC folder structure, same as default MVC folder structure. For example, large enterprise application may have different modules like admin, accountant, marketing etc. So, an Area can contain separate MVC folder structure for all these modules as shown in the snapshot below

asp.net MVC folder structure

How to Create an Area

You can create an Area using ASP.NET MVC 5 and Visual Studio 2013 and above for web by right clicking on the project in the solution explorer -> Add -> Area, and then entering the area name in the pop-up box shown as illustrated below

asp.net How to Create an Area

Enter your choice Area name into the Add Area dialogue box and click Add to complete the Area creation.

asp.net Area name into the Add Area dialogue box

From the Areas created as shown in the screenshot, you will observer that each area extends an AreaRegistration class in {area name} + AreaRegistration.cs file.

The following is AdminAreaRegistration class created with Admin area.

Area Registration:

using System.Web.Mvc;
namespace ActivityTab.Areas.Admin
{
    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 }
            );
        }
    }
}

AreaRegistration class overrides RegisterArea method to map the routes for the area. In the above example, any URL that starts with admin will be handled by the controllers included in the admin folder structure under Area folder. For example, http://localhost/admin/index will be handled by profile controller included in Areas/admin/controller/IndexController folder.

Finally, all the area must be registered in Application_Start event in Global.asax.cs using the AreaRegistration.RegisterAllAreas(); function

Thus using the Area technique, we can handle very complex projects, splitting the different components into different areas.