1

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.

1 Answer 1

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

It worked! I've been struggling with this for days. Thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.