ASP 无法找到核心路由 POST 页面
ASP Core Routing POST page can't be found
我有标准的 asp 核心路由设置:
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
然后我有4个动作(2个get和2个post):
public async Task<IActionResult> Edit(int? id)
{...}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, CustomDTO customDTO)
{...}
public async Task<IActionResult> Review(int? id)
{...}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Review(int id, DifferentCustomDTO differentCustomDTO)
{...}
评论表单剃须刀是这样开始的:
<form asp-action="Review">
两个 url 看起来像这样:
https://localhost/MyController/Review/12
在正常情况下,POST 操作会从 url 值中获取其 ID,并从表单提交的数据中获取 CustomDTO。这一直有效,直到最近我开始得到这个:
This localhost page can’t be found No webpage was found for the web address:
https://localhost/MyController/Review/12
HTTP ERROR 404
我尝试删除 4 个中的 2 个以查看是否存在某种冲突,但这并没有改变任何东西。
我怎样才能找到我搞砸的东西?
您需要向 post 主体添加一些数据,因为如果您使用此 URL:
https://localhost/MyController/Review/12
然后 HTML 请求的 post 主体将为空。尝试将隐藏字段添加到表单中,例如:
@Html.HiddenFor(e => e.Id)
我有标准的 asp 核心路由设置:
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
然后我有4个动作(2个get和2个post):
public async Task<IActionResult> Edit(int? id)
{...}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, CustomDTO customDTO)
{...}
public async Task<IActionResult> Review(int? id)
{...}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Review(int id, DifferentCustomDTO differentCustomDTO)
{...}
评论表单剃须刀是这样开始的:
<form asp-action="Review">
两个 url 看起来像这样:
https://localhost/MyController/Review/12
在正常情况下,POST 操作会从 url 值中获取其 ID,并从表单提交的数据中获取 CustomDTO。这一直有效,直到最近我开始得到这个:
This localhost page can’t be found No webpage was found for the web address:
https://localhost/MyController/Review/12
HTTP ERROR 404
我尝试删除 4 个中的 2 个以查看是否存在某种冲突,但这并没有改变任何东西。 我怎样才能找到我搞砸的东西?
您需要向 post 主体添加一些数据,因为如果您使用此 URL:
https://localhost/MyController/Review/12
然后 HTML 请求的 post 主体将为空。尝试将隐藏字段添加到表单中,例如:
@Html.HiddenFor(e => e.Id)