What is the purpose of the asp-area attribute in ASP.NET Core MVC Web?

3

Creating an ASP.NET Core MVC Web project in Visual Studio 2017 , I found that in the _Layout.cshtml a has an attribute called asp-area but it has no value:

<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>

After all, what is the purpose of this attribute?

    
asked by anonymous 05.06.2018 / 12:23

1 answer

3

Basically to inform you that a link / route refers to an area
ASP.NET Core areas

As ASP.NET will look for a controller in the Controllers folder in the root of the site, if you want to use a controller that is in a specific area needs to report using the asp-area attribute.

Example:

/
/Areas
      /Admin
             /Controllers
                          /UsuarioController.cs
/Controllers
            /LoginController.cs

To link to the controller at the root, just use asp-area=""

<a asp-area="" asp-controller="Login" asp-action="Index">Logar</a>

For the controller that is in the Area Admin, use asp-area="Admin"

<a asp-area="Admin" asp-controller="Usuario" asp-action="Index">Listar Usuários</a>

In your example then HomeController is not in an Area , but in the Controllers folder in the project root.

    
05.06.2018 / 13:18