Working With Areas In ASP.NET Core 3.1


Hii guys! This article explains how to create areas in core 3.1. We will make multiple areas and call them from the views. You'll also learn to set routing of areas.

What are Areas?

  • Areas are part of MVC, which is a separate module in our application.
  • We can create a level application by diving the modules areawise.
  • We can create the same name controller, views at multiple times.
  • We can create areas for like Employee, Admin, Customer, etc.
  • There is a shared folder like the MVC, which contains the _Layout of the application. and _ViewStart, and _ViewImport views in the views folder.
While executing any view, the _Layout called, but before it, _ViewStart and _ViewImport called. _ViewStart contains the "_Layout" path that is auto-applied to every view page without adding it to every view. _ViewImport contains all the tag helpers and the namespaces which we want to use over the view page for ignoring to adding it multiple times.
Okay, let's see how to create it.
Step 1
Right click on Areas folder > Add > Area> Give it name Admin >ok
Step 2
In .net core, the Shared folder is not auto-created so, we have to create it manually by copying the Shared folder in the Admin Areas Views folder.
Step 3
Because Area is the MVC concept, for enabling areas in .Net Core, we have to register MVC . And EnableEndpointRouting is set to false in the startup.cs file.
  1. services.AddMvc(options => options.EnableEndpointRouting = false);  
.Net Core auto-registered controllers with views and the razor pages, but if it is not registered, in your web application, in any case,  please register it like this.
  1. services.AddControllersWithViews();  
  2. services.AddRazorPages();  
Step 4
Now we set the routing of areas so we can call it.
Routing- routing is the path of URL by which we can easily redirect to any view page from view to view or controller to view or controller to a controller of any area.
Embed the following tag in the Configure method for setting the route of the area in your startup.cs file.
  1. endpoints.MapAreaControllerRoute(  
  2.                   name: "Admin",  
  3.                   areaName: "Admin",  
  4.                   pattern: "Admin/{controller=Home}/{action=Index}");  
  1. endpoints.MapAreaControllerRoute(  
  2.                  name: "Customer",  
  3.                  areaName: "Customer",  
  4.                  pattern: "Customer/{controller=Home}/{action=Index}");    
Here is the full configuration method with default routing and Admin area routing.
  1. app.UseEndpoints(endpoints =>  
  2.           {  
  3.               endpoints.MapAreaControllerRoute(  
  4.                  name: "Admin",  
  5.                  areaName: "Admin",  
  6.                  pattern: "Admin/{controller=Home}/{action=Index}");  
  8.               endpoints.MapControllerRoute(  
  9.                   name: "default",  
  10.                   pattern: "{controller=Home}/{action=Index}/{id?}");  
  12.               endpoints.MapRazorPages();  
  13.           });  
Make sure you embed any area routing before default routing. 
Step 5
Okay, now I created the Home controller and its index method in the Admin area and the Customer Area.
.Net core assumes every controller as a simple controller. So, for identifying each area's controller, we will decorate the area's controller with the [Area("admin")] attribute. 
  1. [Area("admin")]  
  2.     public class HomeController : Controller  
  3.     {  
  4.         public IActionResult Index()  
  5.         {  
  6.             return View();  
  7.         }  
  8.     }  
  1. [Area("customer")]  
  2. public class HomeController : Controller  
  3. {  
  4.     public IActionResult Index()  
  5.     {  
  6.         return View();  
  7.     }  
  8. }  
Step 6
Let us see how to call any area from the view.
Way 1
  1. <a asp-area="admin" asp-controller="Home" asp-action="index">Go to Admin Area</a>  
  1. <a asp-area="customer" asp-controller="Home" asp-action="index">Go to Customer Area</a>  
Way 2
  1. <a href="@Url.Action("index","home",new {Area="admin"})">Go to Admin Area</a>  
  1. <a href="@Url.Action("index","home",new {Area="customer"})">Go to Customer Area</a>   
Way 3
  1. <a href="/admin/Home/index">Go to Admin Area</a>  
  1. <a href="/customer/Home/index">Go to Customer Area</a>  
Call the front index view like this
  1. <a asp-action="index" asp-controller="home">Go to Home</a>  


In this article, I discussed how we create areas in .net core so that we can separate modules in the application. Furthermore, we saw how to work with multiple areas and set the routing. At last, we saw the calling of two different areas from the view.