1
votes

im using .net core 3.1.1

in mvc area when i use asp-action like this

<a  asp-action="Index2">Index2</a>

do not generate link correctly (missed area name )

generate link : http://localhost:49770/Home/Index2

but correct link is http://localhost:49770/admin/Home/Index2

i using endpoint routing

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

            endpoints.MapAreaControllerRoute(
                "area",
                "Admin",
                "{area:exists}/{controller=Home}/{action=Index}/{id?}"

            );
        });

but when is use older routing (usemvc) asp-action generate link correctly

http://localhost:49770/admin/Home/Index2

 app.UseMvc(routes =>
    {
      routes.MapRoute(
        name : "areas",
        template : "{area:exists}/{controller=Home}/{action=Index}/{id?}"
      );
    });

anyone can help me with endpoint routing use asp-action thanks

1
Hello, and welcome to Stack Overflow. If I'm understanding you right, I am assuming that these links are from Razor views from within the area, and you're expecting them to implicitly include the area name in the URL since that should be assumed given the context. Is that correct?Jeremy Caney
Generally, though, you would usually use the asp-area tag helper in conjunction with asp-action to specify the target area (source).Jeremy Caney

1 Answers

1
votes

You should modify the order to make the Areas route first :

endpoints.MapAreaControllerRoute(
    "area",
    "Admin",
    "{area:exists}/{controller=Home}/{action=Index}/{id?}"


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

So that the <a asp-action="Index2">Index2</a> will create the link to an action method of the same area .