What Are Areas in ASP.Net MVC - Part 6

Before reading this article, I highly recommend reading my previous parts.

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 MVCs. like.

 MVC applications

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.

Area Registration.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.

ASP.Net MVC 5.0

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 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 of 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.

Index 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 run the application 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 we can manually.

Home

Now open your default “RouteConfig.cs” to set the namespaces attribute with the 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.

Home controller

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

 Area Controller

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”.

Data tokens

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

Woman index

Thanks for Reading.

Nitin Pandit

Twitter: @thinkaboutnitin


Similar Articles