ASP.Net MVC Areas

Introduction

Areas allows you to separate your large projects into more distinct partitions. Its like having a sub project inside your main project. All your Controllers, Views and Models for a specific module will belong to one directory structure. To create an area simply right click on anywhere in your project in solution explorer and click on Add – > Area The area will create in a new ”Areas” Folder.

Once created you can see the all familiar folder structure like Controllers, Views and Models inside the newly created Area folder by name. you can also see a new AreaRegistration.cs starting with your area name. this class is derived from AreaRegistration class in System.Web.Mvc and contains one overridden property  AreaName and another overridden method RegisterArea. the property will return your Area name and the method registered the routes we define under the area. creating an area also calls AreaRegistration.RegisterAllAreas in Application_start method in Global.asax.cs.

Content of AdminAreaRegistration.cs


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 });
}
}

Content Of Application_Start


protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}

Leave a Reply

Your email address will not be published. Required fields are marked *