Form Tag Helper 如何确定呈现的表单操作?

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

我正在使用 ASP.NET 使用 MVC 的 CORE。我有一个名为 'Profile' 的 Area 设置,其中 UserController 具有 Edit 操作。我不觉得我在这里开辟了任何新天地。

如果我做到这一点 URL:

/Profile/User/Edit/100

我的视图如下所示:

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

为什么 HTML 渲染成这样:

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

而不是这个(注意“/100”):

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

我知道我可以回避 Form Tag Helper 或以其他方式解决它,但看起来应该 'just work'。

我的路由当前是这样配置的:

        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?}");
        });

排序路线时,请确保将段数较多的路线放在段数较少的路线之前。还有为什么你需要上面 2 条看起来相似的路线?

能不能像下面这样修改路由试试?

    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?}");
    });