[英]Multiple forms on an asp.net core 2.2+ web application
Can someone explain the following unexpected behaviour.有人可以解释以下意外行为吗? I've tested this in both core 2.2 and 3.1.
我已经在核心 2.2 和 3.1 中对此进行了测试。
I'm trying to make sense of posting 2 different forms on the same razor page.我试图在同一个剃须刀页面上发布 2 个不同的表单。
Here's the HTML:这是 HTML:
<form method="post">
<button type="submit">post</button>
</form>
<form method="post">
<button type="submit" asp-page-handler="WTF" >post wtf</button>
</form>
Here's the razor page behind:这是后面的剃刀页面:
public void OnGet()
{
Debug.WriteLine("Get");
}
public void OnPost()
{
Debug.WriteLine("Post");
}
public void OnPostWTF()
{
Debug.WriteLine("PostWTF");
}
I expect that when I press the 'post' button, the OnPost action gets called.我希望当我按下“发布”按钮时,会调用 OnPost 操作。 When I press the 'post wtf' button, the OnPostWTF action gets called.
当我按下“post wtf”按钮时,会调用 OnPostWTF 操作。 That kind of happens.
有这种事。 If I press the 'post' button initially, the expected action is called.
如果我最初按下“发布”按钮,则会调用预期的操作。 But, as soon as I press the 'post wtf' button, ALL subsequent posts only call the OnPostWTF action, regardless which button is pressed!
但是,一旦我按下“post wtf”按钮,所有后续帖子只会调用 OnPostWTF 操作,无论按下哪个按钮!
Very nice guide here: https://www.learnrazorpages.com/razor-pages/handler-methods非常好的指南: https : //www.learnrazorpages.com/razor-pages/handler-methods
The name of the handler is added to the form's action as a query string parameter.
处理程序的名称作为查询字符串参数添加到表单的操作中。
So when you are posting without a handler then the current url is being posted to.因此,当您在没有处理程序的情况下发布时,当前 url 将被发布到。
In your case it will be the one you have switched to with the query parameter handler=WTF
.在您的情况下,它将是您使用查询参数
handler=WTF
切换到的那个。
The best practice when you have multiple forms in the same page, is to setup a handler for each request to avoid such troubles.当您在同一页面中有多个表单时,最佳实践是为每个请求设置一个处理程序以避免此类麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.