简体   繁体   English

asp.net core 2.2+ web 应用程序上的多个表单

[英]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.

相关问题 这是如何使用Entity Framework Core和ASP.NET Core MVC 2.2+和3.0创建数据传输对象(DTO) - Is This How to Create a Data Transfer Object (DTO) with Entity Framework Core & ASP.NET Core MVC 2.2+ and 3.0 一个使用 Azure AD B2C 的多个客户端 ID(应用程序 ID)的 ASP.NET Core 2.2 Web API 应用程序 - One ASP.NET Core 2.2 Web API app Using Multiple Client IDs (Application IDs) of Azure AD B2C 我可以将 ASP.NET Core MVC 添加到现有的 ASP.NET Web Z645024253191A381C36Z83CAE8 - Can I add ASP.NET Core MVC to existing ASP.NET Web Forms Application 如何在ASP.Net Core 2.2 Web Api应用程序中连接到Amazon Kinesis Firehose - How to connect to Amazon Kinesis Firehose in ASP.Net Core 2.2 Web Api Application 使用 .NET Core Web API 作为 ASP.NET Web Forms 应用程序的子路由 - Using .NET Core Web API as subroute of ASP.NET Web Forms Application ASP.NET CORE 2.2 WEB APP 中的 Session 过期问题 - Session expire problen in ASP.NET CORE 2.2 WEB APP ASP.Net Core 2.2 Web App中的自定义身份 - Custom Identity in ASP.Net Core 2.2 Web App 缺少“ASP.NET Core Web 应用程序(.NET Core)”模板 - missing “ASP.NET Core Web Application (.NET Core)” template ASP.Net Core 2.2 身份验证 - ASP.Net Core 2.2 Authentication ASP.NET Core 2.2 JWT身份验证 - ASP.NET Core 2.2 JWT Authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM