Introduction
Hi guys! This article explains how to create areas in asp.net core 3.1. We will make multiple areas and call them from the views. You'll also learn to set the 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 and view it multiple times.
- We can create areas for Employees, Admin, Customers, 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 is called, but before it, _ViewStart and _ViewImport are 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 that we want to use over the view page for ignoring to add 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.
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.
services.AddControllersWithViews();
services.AddRazorPages();
Step 4. Now we set the routing of areas so we can call it.
Routing- routing is the path of the 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.
endpoints.MapAreaControllerRoute(
name: "Admin",
areaName: "Admin",
pattern: "Admin/{controller=Home}/{action=Index}"
);




antik flwrPosted Mar 11, 2022, 2:25 PM
{area:exists} : what doing exists constraint on area routing?
Greg HoodPosted Feb 26, 2022, 3:23 PM
This was a big help! Thank you!
samir bhattPosted Feb 7, 2022, 12:44 PM
You should have started from the very first step of creating the solutions and then work from there Step by Step rather than straight way continuing with adding Areas.