0

I have a problem in ASP.NET Core 5. When I use asp-area for example like this:

<form  asp-area="Admin" asp-controller="User" asp-action="Register">your text

the generated link for the code above is

https://localhost:44300/User/Register?area=Admin

though I want it to go

https://localhost:44300/Admin/User/Register

Am I doing someting wrong? Btw this my route in startup.cs:

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
                name: "areaRoute",
                pattern: "{area}/{controller=Home}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
              name: "areas",
              pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

        }
);
2
  • 1
    Have you also associated your controller with the area? learn.microsoft.com/en-us/aspnet/core/mvc/controllers/… Commented Oct 21 at 17:30
  • 1
    Also what is the purpose of this route: endpoints.MapControllerRoute( name: "areaRoute", pattern: "{area}/{controller=Home}/{action=Index}/{id?}"); ? The documentation and examples don't seem to regard this part as necessary. Just the last route you've specified ought to do the trick as far as I can see. Commented Oct 21 at 17:31

1 Answer 1

3

Make sure that the area routes should be placed earlier as stated in the docs

Area routes typically use conventional routing rather than attribute routing. Conventional routing is order-dependent. In general, routes with areas should be placed earlier in the route table as they're more specific than routes without an area.

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "areas",
        pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
})

Assume that you have applied that Area attribute to the controller:

[Area("Admin")]
public class UserController : Controller
{
    [HttpPost("Register")]
    public IActionResult Register(RegisterInputDto input)
    {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

yes i already done those im in a view of a admin area rn and i wanna jump to another action of a controller in a admin area and without asp-area it the generated link is .com/DashBoard/Index which i want it to be .com/Admin/DashBoard/index
Yes, if the API/page route is from the Area controller, you must provide the asp-area tag helper.
check the form code i wrote i write it but generates wrong route for me
Sounds odd, I wonder whether you have multiple area controllers, it would be great if you could attach the latest Program.cs in the question. And minimal code to reproduce

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.