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.
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.
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
- using System.Web.Mvc;
- namespace MvcArea.Areas.Article {
- public class ArticleAreaRegistration: AreaRegistration {
- public override string AreaName {
- get {
- return "Article";
- }
- }
- public override void RegisterArea(AreaRegistrationContext context) {
- context.MapRoute("Article_default", "Article/{controller}/{action}/{id}", new {
- action = "Index", id = UrlParameter.Optional
- });
- }
- }
- }
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace MvcArea {
- public class MvcApplication: System.Web.HttpApplication {
- protected void Application_Start() {
-
- AreaRegistration.RegisterAllAreas();
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- }
- }
- }
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.
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace MvcArea {
- public class RouteConfig {
- public static void RegisterRoutes(RouteCollection routes) {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {
- controller = "Home", action = "Index", id = UrlParameter.Optional
- }, namespaces: new [] {
- "MvcArea.Controllers"
- });
- }
- }
- }
Step by Step Demo
Create an empty ASP.NET MVC web application.
I have given this project name as ”MvcArea”.
Now, we are going to add Controller named “HOME” under MainArea(Root). Right click on Project and select ADD-->Controller.
HomeController.cs Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcArea.Controllers {
- public class HomeController: Controller {
-
- public ActionResult Index() {
- return View();
- }
- }
- }
Now, right click on INDEX ActionMethod of Home Controller.
Now, we are going to add AREA named “Article”. Right click on Project and select ADD-->AREA.
Now, right click on ARTICLE area and select ADD-->CONTROLLER
Article Area’s HomeController Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcArea.Areas.Article.Controllers {
- public class HomeController: Controller {
-
- public ActionResult Index() {
- return View();
- }
- }
- }
Now, right click on INDEX ActionMethod of Home Controller of Article.
Now, we are going to add AREA named “BLOG”. Follow the same suite as in the above steps.
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”.
After successfully creating the Areas, Controllers and Views (index) of ARTICLE, BLOG and QA, your Solution Explorer will look like this.
Now, run the project by pressing F5. You will get this exception.
RouteConfig.cs Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace MvcArea {
- public class RouteConfig {
- public static void RegisterRoutes(RouteCollection routes) {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {
- controller = "Home", action = "Index", id = UrlParameter.Optional
- }, namespaces: new [] {
- "MvcArea.Controllers"
- });
- }
- }
- }
To resolve this, you have to just add the underline marked text in ROUTECONFIG.CS.
Now, run the application by pressing F5. The following will be the output.
OUTPUT
MainArea(Root) Home Controller Index ActionMethod
ARTICLE area Home Controller Index ActionMethod
BLOG area Home Controller Index ActionMethod
QA area Home Controller Index ActionMethod
Thank you for reading this article. I hope it helps you someday. Please give your valuable comments and feedback.