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

Asked

Viewed 469 times

3

Creating a project Web ASP.NET Core MVC in the Visual Studio 2017, I found that in the file _Layout.cshtml all elements 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?

1 answer

4


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

Like the ASP.NET go find a controller in the briefcase Controllers at the root of the site, if you want to use a controller which is in a specific area need to inform using the attribute asp-area.

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>

To the controller on Area Admin, use asp-area="Admin"

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

In his example then, the Homecontroller is not in a Area, but in the briefcase Controllers at the root of the project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.