Working With Areas In ASP.NET Core 3.1

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

Area

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.

View

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}"
);
endpoints.MapAreaControllerRoute(
    name: "Customer",
    areaName: "Customer",
    pattern: "Customer/{controller=Home}/{action=Index}"
);

Here is the full configuration method with default routing and Admin area routing.

app.UseEndpoints(endpoints =>
{
    endpoints.MapAreaControllerRoute(
        name: "Admin",
        areaName: "Admin",
        pattern: "Admin/{controller=Home}/{action=Index}"
    );

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}"
    );

    endpoints.MapRazorPages();
});

Note. 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 is a simple controller. So, to identify each area's controller, we will decorate the area's controller with the [Area("admin")] attribute.

[Area("admin")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}
[Area("customer")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Step 6. Let us see how to call any area from the view.

Way 1

<a asp-area="admin" asp-controller="Home" asp-action="index">Go to Admin Area</a>
<a asp-area="customer" asp-controller="Home" asp-action="index">Go to Customer Area</a>

Way 2

<a href="@Url.Action("index","home",new {Area="admin"})">Go to Admin Area</a>  
<a href="@Url.Action("index","home",new {Area="customer"})">Go to Customer Area</a>   

Way 3

<a href="/admin/Home/index">Go to Admin Area</a>  
<a href="/customer/Home/index">Go to Customer Area</a>  

Call the front index view like this

<a asp-action="index" asp-controller="home">Go to Home</a>  

Output

Go to home

Admin area index

Customer area index

Summary

In this article, I discussed how we create areas in the .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.