Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Browse Tutorials

02 Intermediate

MVC Areas with example

MVC Areas with example

05 Mar 2024
Intermediate
187K Views
2 min read

Areas was introduced in Asp.net MVC2 which allow us to organize models, views, and controllers into separate functional sections of the application, such as administration, billing, customer support, and so on. This is very helpful in a large web application, where all the controllers, views, and models have a single set of folders and that become difficult to manage.

Each MVC area has its own folder structure which allow us to keep separate controllers, views, and models. This also helps the multiple developers to work on the same web application without interfere to one another.

Registering Area

Before working with area, make sure you have registered your area with in the Application_Start method in Global.asax as shown below.

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

}

Note

Always remember the order of registering the Areas must be on top, so that all of the settings, filters and routes registered for the applications will also apply on the Areas.

Creating Area

To add an area to an MVC application, right-click on the project item with in the Solution Explorer window and select Add>Area option as shown below.

Now a new prompt will appear, with in give the name of the area like "Department" and click Add button. Now the new Department Area has been cretaed as shown in below fig.

Now you have seen DepartmentAreaRegistration class has been created and in the RegisterArea method, a route for the area has been defined as shown below.

using System.Web.Mvc;
namespace Mvc4Area.Areas.Department
{
 public class DepartmentAreaRegistration : AreaRegistration
 {
 public override string AreaName
 {
 get
 {
 return "Department";
 }
 }
 
 public override void RegisterArea(AreaRegistrationContext context)
 {
 context.MapRoute(
 "Department_default",
 "Department/{controller}/{action}/{id}",
 new { action = "Index", id = UrlParameter.Optional }
 );
 }
 }
}

Populating Area

Now you can create controllers, views, and models in the Department area like as below.

How to see view with in Areas

Now, let's see how to view your Index view with the help of Page Inspector as shown below.

What do you think?

I hope you have got what is Areas and how to use it with in the application. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this