【问题标题】:ASP Core Routing POST page can't be found找不到 ASP 核心路由 POST 页面
【发布时间】:2020-11-27 07:52:18
【问题描述】:

我有标准的 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 个获取和 2 个发布):

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)
{...}

Review form razor 是这样开始的:

<form asp-action="Review">

两个网址都如下所示:

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 个以查看是否存在某种冲突,但这并没有改变任何东西。 我怎样才能找到我搞砸的东西?

【问题讨论】:

  • This was working until recently I started getting this您是否进行了任何更改,它只是不适用于此控制器操作吗?你能告诉我们整个表单和控制器吗?

标签: asp.net-mvc asp.net-core asp.net-mvc-routing


【解决方案1】:

您需要在帖子正文中添加一些数据,因为如果您使用的是此 URL:

https://localhost/MyController/Review/12

那么 post 请求的 HTML Body 将为空。尝试在表单中添加隐藏字段,例如:

@Html.HiddenFor(e => e.Id)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2018-06-26
    • 2012-04-30
    • 1970-01-01
    • 2016-10-20
    • 2013-08-12
    • 1970-01-01
    相关资源
    最近更新 更多