ASP.NET MVC Areas

From ASP.Net MVC 2.0 Microsoft provided a new feature in MVC applications, Areas. Areas are just a way to divide or “isolate” the modules of large applications in multiple or separated MVC. like:


MVC

When you add an area to a project, a route for the area is defined in an AreaRegistration file. The route sends requests to the area based on the request URL. To register routes for areas, you add code to the Global.asax file that can automatically find the area routes in the AreaRegistration file.

AreaRegistration.RegisterAllAreas();

Benefits of Area in MVC

  1. Allows us to organize models, views and controllers into separate functional sections of the application, such as administration, billing, customer support and much more.

  2. Easy to integrate with other Areas created by another.

  3. Easy for unit testing.

How to create an Area in ASP.Net MVC 5

I have an ASP.Net MVC 5.0 Application with a default Home controller and Index action with their view also, see the solution of the project.

create Area in ASP

And now I need to use an area in that application, so let's see how to add one.

Right-click on the Project then select Add -> Area.

Add Area

Give a name for what you want to add. Assume I am creating a website for online shopping and I have two modules, one for each of men and women so I'll add two areas for men and women.

man vs woman

First Area For Men.

First Area For Men

It'll add a folder for the name as Area and that has all the folders for Model, View and Controller.

add a folder

Now add a Controller in the Men's area.

add an Controller

Write the name for the Controller.

name for Controller

It has a default Action with the name of the Index.

default Action

And just add the view for the Index Action in the Men's area.

ndex Action in Men Area

So finally I have everything with the View and Controllers in the solutions as in the following:

Controllers

Now do the entire procedure the same for the next area for Women.

next Area

After adding everything the same as the previous area for Men, now see the solutions.

Area for Men

Now if I'll run the application so the request will hit the default URL register in “RouteConfig.cs” which is http://.../Home/Index so it'll return exceptions.

error

See the exception message that shows that we have multiple “Home” controllers in the same project so use the namespaces attribute in the default route in “RouteConfig.cs” to remove this exception. If I need to request directly to any Area so we can manually.

exception

Now open your default “RouteConfig.cs” to set the namespaces attribute with default route.

default route

Add this:

namespaces: new string[] {"MVC5WithArea.Controllers"}

namespaces

Now run the application again. This is runnable now, it'll find the Home controller using namespaces.

controller

Now if you want to, redirect the request from the default controller to the Area Controller so I will add an action link.

add an action

But the preceding action link can only request in the same domain or in the same area but if I want to request a different area then use this overload of the Action Link extension method.

Action Link extension

@Html.ActionLink("Go To Mens Home", "Index", "Home",new {area="Mens" },null)

Just add to the Action link for a different area one for Men and another for Women.

Action link

Now run The application and click on any link to redirect the action.

Run The Application

This was the way to redirect from one to another area.

If I want to set the default area argument in the URL then use a DataTokens collection to add a key for the area in “RouteConfig.cs”.

RouteConfig

Now run it again and see the default view from an Area.

woman index

Thanks for Reading.

Author
Nitin Pandit
13 51.9k 31.4m
Next » Partial Views In ASP.NET MVC