Search code examples
asp.net-core.net-coreasp.net-core-mvcasp.net-core-1.0

How does the Form Tag Helper determine the rendered form action?


I'm using ASP.NET CORE using MVC. I've got an Area setup called 'Profile', with a UserController that has an Edit action. I don't feel I'm breaking any new ground here.

If I got to this URL:

/Profile/User/Edit/100

And I have a view that looks like this:

<form method="post" role="form" asp-controller="User" asp-action="Edit">

Why does the HTML render like this:

<form method="post" role="form" action="/Profile/User/Edit">

Instead of this (Note the "/100"):

<form method="post" role="form" action="/Profile/User/Edit/100">

I realize I can side-step the Form Tag Helper or otherwise work around it, but it seems like this should 'just work'.

This is how my routes are currently configured:

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

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

Solution

  • When ordering routes, make sure to put the routes with larger number of segments before routes with lesser number of segments. Also why do you need 2 similar looking routes above?

    Can you try after modifying routes like below?

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