asp.net-coreasp.net-core-mvcasp.net-core-tag-helpers

ASP.NET Core area link ( /?Area=admin )


I created an area, but I have a problem. I use redirection with @url.action or tag helpers in the form tag.

<a asp-action="AddPage" asp-controller="Orders" asp-area="admin">Link</a>

or

<a href="@Url.Action("Index","Home",new{Area="admin"})">Admin Sayfasi</a>

But it keeps redirecting me to a page like this.

https://localhost:44357/?Area=admin

When I check what these codes turn into through the browser, I see something like this.

<a href="/Orders/AddPage?area=admin">Link</a>
<a href="/?Area=admin">Admin Sayfasi</a>

I don't actually have any visible code errors for this to happen, but I can't find the reason why.

My endpoints extension in my Startup.cs file is as follows:

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

Can you help me how to solve this problem?

Thank you in advance for your help.


Solution

  • From the Add Area Route documentation:

    In general, routes with areas should be placed earlier in the route table as they're more specific than routes without an area.

    You need to place the Area routes at the beginning of the route table.

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

    enter image description here