The ASP.NET Core MVC tag helpers aren't acting like they used to. When I write something like this:
<a asp-area="Admin" asp-controller="Home" asp-action="Index"></a>
The link redirects me to ~/Home/Index?area=Admin
instead of ~/Admin/Home/Index
(which can be simplified to ~/Admin
).
I have decorated the controllers in that area with [Area("Admin")]
, the tag helpers are imported in my _ViewImports
inside ~/Areas/Admin/Views
folder and my routing configuration looks like this:
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "areaRoute",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
I can't figure out what I did wrong. I didn't have this problem before.
This is happening due to the order of the route configurations in your Program.cs
. It seems the default
route is being matched before the areaRoute
, causing the URL to be generated incorrectly.
I suggest you try the code below and see if it works well or not.
app.MapControllerRoute(
name: "areaRoute",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
In the view:
<a asp-area="Admin" asp-controller="Test" asp-action="Index">test</a>
Result: