ASP.NET Core MVC Interview Question #88

How do you create a link to an action inside an Area?

Advanced Routing, Filters, Areas & Application Structure Mid-Level Intermediate

Quick Interview Answer

Use the asp-area Tag Helper together with asp-controller and asp-action. This ensures that URL generation includes the intended area route value.

Detailed Explanation

Use the asp-area Tag Helper together with asp-controller and asp-action. This ensures that URL generation includes the intended area route value.

When linking from an area back to a non-area controller, asp-area can be set to an empty value so the current area's ambient route value does not accidentally remain in the generated URL.

Code Example

<a asp-area="Admin"
   asp-controller="Dashboard"
   asp-action="Index">
    Admin Dashboard
</a>

<a asp-area=""
   asp-controller="Home"
   asp-action="Index">
    Public Home
</a>