Getting Started With Area In ASP.NET MVC

 In this article, you will get answers  to the following questions along with a step-by-step demo.

  • What is Area in ASP.NET MVC?
  • Advantages of Area
  • How to create Area?
  • What is <AREA Name>AreaRegistration.cs file?
  • How to register Area into Global.asax
  • Can we create a same name Controller across all the areas including root (MainArea)? If yes, how to handle it?

What is Area?

In ASP.NET MVC, you can classify the working division of the website section, that is called Area. Area was introduced in ASP.NET MVC version 2. Each Area can have seperate Model, View, and Controller, as well as Web.Config file.

Advantages of Area

Area helps us write a clean seperated code which helps us a lot in easy maintenance and super easy testing. Area functionalities help us to organize the code very efficiently and effectively. You can create n number of Area as per your requirement.

MVC
How to create AREA?

After creation of a project, right click on the Solution Explorer and select ADD-->AREA.  You will see a screen similar to the following screenshot.

MVC

MVC

MVC

What is <AREA Name>AreaRegistration.cs file?

<AREA Name> is equal to your area name. The name of the area can be anything that you want to create. After successfully creating the area, system creates this file with your area name.

AreaRegistration file contains the class which overrides AreaName properties and RegisterArea method to map the routes area. And, this file helps us in configuring the route as per requirement.

Example

I have created an area called “Article”. In this case, the system will create a file called “ArticleAreaRegistration.cs”.

ArticleAreaRegistration.cs Code 

  1. using System.Web.Mvc;  
  2. namespace MvcArea.Areas.Article {  
  3.     public class ArticleAreaRegistration: AreaRegistration {  
  4.         public override string AreaName {  
  5.             get {  
  6.                 return "Article";  
  7.             }  
  8.         }  
  9.         public override void RegisterArea(AreaRegistrationContext context) {  
  10.             context.MapRoute("Article_default""Article/{controller}/{action}/{id}"new {  
  11.                 action = "Index", id = UrlParameter.Optional  
  12.             });  
  13.         }  
  14.     }  
  15. }   

 MVC

How to register Area into Global.asax?

We have no need to manually attach the Areas into Global.asax file. It gets attached by default. But all the areas must be registered in Application_Start event of the Global.asax.cs file.

Please see the screenshot.

Global.asax.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace MvcArea {  
  8.     public class MvcApplication: System.Web.HttpApplication {  
  9.         protected void Application_Start() {  
  10.             //AREA attached  
  11.             AreaRegistration.RegisterAllAreas();  
  12.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  13.         }  
  14.     }  
  15. }  

MVC

Can we create a same name Controller across all the areas including root (MainArea)? If yes, How to Handle it?

Yes, we can create the same name Controller across all the areas including root (MainArea). But it's not runnable / executable without doing some settings in the code.

It throws the exception. Because the same name Controllers exist in all the areas including main area (root), the routing system gets confused to load the Controller by default. In the routing table, we mention Controller name and ActionMethod name. To come-out from this problem, you have to add namespace in route configuration, and that's it. Your problem gets resolved.

MVC

MVC

To resolve this exception you have to just add this line in ROUTECONFIG.CS file.

I had underlined and increased font size of that line.

ROUTECONFIG.CS   Code  

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace MvcArea {  
  8.     public class RouteConfig {  
  9.         public static void RegisterRoutes(RouteCollection routes) {  
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  11.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  12.                 controller = "Home", action = "Index", id = UrlParameter.Optional  
  13.             }, namespaces: new [] {  
  14.                 "MvcArea.Controllers"  
  15.             });  
  16.         }  
  17.     }  
  18. }   

MVC
Step by Step Demo 

Create an empty ASP.NET MVC web application.

MVC

I have given this project name as ”MvcArea”.

MVC

MVC

Now, we are going to add Controller named “HOME” under MainArea(Root). Right click on Project and select ADD-->Controller.

HomeController.cs Code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace MvcArea.Controllers {  
  7.     public class HomeController: Controller {  
  8.         // GET: Home  
  9.         public ActionResult Index() {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }   

MVC

Now, right click on INDEX ActionMethod of Home Controller.

MVC

MVC

Now, we are going to add AREA named “Article”. Right click on Project and select ADD-->AREA.

MVC

MVC

Now, right click on ARTICLE area and select ADD-->CONTROLLER

MVC

MVC

Article Area’s  HomeController Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace MvcArea.Areas.Article.Controllers {  
  7.     public class HomeController: Controller {  
  8.         // GET: Article/Home  
  9.         public ActionResult Index() {  
  10.             return View();  
  11.         }  
  12.     }  
  13. }  

MVC

Now, right click on INDEX ActionMethod of Home Controller of Article.

MVC

MVC

Now, we are going to add AREA named “BLOG”. Follow the same suite as in the above steps. 

MVC

Create a HOME Controller and Index View for BLOG area, as we created for ARTICLE area.

Now, we are going to add AREA named “QA”.

MVC

After successfully creating the Areas, Controllers and Views (index) of ARTICLE, BLOG and QA, your Solution Explorer will look like this.

MVC

Now, run the project by pressing F5. You will get this exception.

MVC

RouteConfig.cs  Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace MvcArea {  
  8.     public class RouteConfig {  
  9.         public static void RegisterRoutes(RouteCollection routes) {  
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  11.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  12.                 controller = "Home", action = "Index", id = UrlParameter.Optional  
  13.             }, namespaces: new [] {  
  14.                 "MvcArea.Controllers"  
  15.             });  
  16.         }  
  17.     }  
  18. }  

To resolve this, you have to just add the underline marked text in ROUTECONFIG.CS.

MVC

Now, run the application by pressing F5. The following will be the output.

OUTPUT

MainArea(Root) Home Controller Index ActionMethod

MVC
ARTICLE area Home Controller Index ActionMethod

MVC
BLOG area Home Controller Index ActionMethod

MVC
QA area Home Controller Index ActionMethod

MVC
Thank you for reading this article. I hope it helps you someday. Please give your valuable comments and feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all