Area in ASP.
NET MVC:
We have already learned that [Link] MVC framework includes separate folders for
Model, View and Controller. However, large application can include a large number of
controller, views and model classes. So to maintain a large number of views, models and
controllers with the default [Link] MVC project structure can become unmanageable
[Link] MVC 2 introduced Area. Area allows us to partition large application into smaller
units where each unit contains separate MVC folder structure, same as default MVC folder
structure.
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.
For example, large enterprise application may have different modules like Admin,
Finance, HR, Marketing etc. So an Area can contain separate MVC folder structure for all
these modules as shown below.
Create Area:
You can create an Area using [Link] MVC and Visual Studio 2015 for web by right
clicking on the project in the solution explorer -> Add -> Area…
Enter Area name in Add Area dialogue box and click Add.
This will add 'Admin' folder under Area folder as shown below.
As you can see, each area includes AreaRegistration class in {area name} +
[Link] file.
The following is AdminAreaRegistration class created with Admin area.
Area Registration:
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
[Link](
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = [Link] }
);
}
}
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, [Link] will be handled by Profile controller
included in Areas/Admin/Controllers/ProfileController folder.
Finally, all the area must be registered in Application_Start event in [Link] as
[Link]();
Registering Area
Before working with area, make sure you have registered your area within the
Application_Start event method in [Link] as shown below:
[Link]:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
namespace MVCEnterpriseApplication
{
public class MvcApplication : [Link]
{
protected void Application_Start()
{
[Link]();
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
}
}
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.
So in this way, you can create and maintain multiple areas for the large application.
Navigating Areas:
[Link] MVC Views often need to link to some action method residing in controller
classes. Unless otherwise specified, action methods and controllers are assumed to be
from the current area. In case you wish to link to an action method belonging to some
other area, here is how you would do just that:
@[Link]("Admin Area", "Index", "Home", new { area = "Admin" }, null)
@[Link]("Finance Area", "Index", "Home", new { area = "Finance" }, null)
@[Link]("HR Area", "Index", "Home", new { area = "HR" }, null)
The above fragment is from the Index view of the Home Controller of Root Level Website.
It renders three hyperlinks using ActionLink(). Notice the fourth parameter that specifies
area name. This parameter governs the area whose action method is being linked to.
If you want to navigate from any area to root level of the website, then you would do
just as following:
@[Link]("Home Page of the Website", "Index", "Home", new { area = "" }, null)
Redirecting to an Action Belonging to another Area
You can also redirect to some action method of a specific area using RedirectToAction()
method. Consider the following action method:
public ActionResult Index()
{
return RedirectToAction("Index", "Home", new { Area = "Admin" });
}
As you can see, the Index() action method of the root level website’s Home Controller
redirects the control to Index() action method of the Home Controller of Admin area.
Summary
[Link] MVC allows you to organize your application into areas. Each area replicates the
MVC structure and can be treated as independent as far as models, views and controllers
are concerned. An area is registered with MVC framework using an area registration class
and RegisterAllAreas() call in the [Link] file.