[英]Razor Pages Post returns 400
I have a ViewComponent on an Index page that makes a post to another page LogTransaction
.我在 Index 页面上有一个 ViewComponent ,它发布到另一个页面LogTransaction
。 When I post to the LogTransaction page, it returns a 400. I know the page is found because when I make a GET request to LogTransaction, it works perfectly.当我发布到 LogTransaction 页面时,它返回 400。我知道该页面已找到,因为当我向 LogTransaction 发出 GET 请求时,它运行良好。 But any time I try to POST to it, it returns a 400.但是每当我尝试向它发布时,它都会返回 400。
Default.cshtml for ViewComponent contained on localhost:5001/Index: localhost:5001/Index 上包含的 ViewComponent 的 Default.cshtml:
@using BudgetTracker.BudgetSquirrel.Web.Pages
@model BudgetViewModel
...
<form action="/LogTransaction" method="post">
<!-- Inputs Here -->
<button type="submit">Save</button>
</form>
LogTransaction.cshtml.cs LogTransaction.cshtml.cs
namespace BudgetTracker.BudgetSquirrel.Web.Pages
{
public class LogTransactionModel : BasePageModel // extends PageModel
{
public void OnGet()
{}
public IActionResult OnPost()
{
Console.WriteLine("Hi");
return Page();
}
}
}
LogTransaction.cshtml日志事务.cshtml
@page
@model BudgetTracker.BudgetSquirrel.Web.Pages.LogTransactionModel
@{
ViewData["Title"] = "LogTransaction";
}
When I click the Save button, I get a 400 on the POST request at /LogTransaction.当我单击“保存”按钮时,我在 /LogTransaction 的 POST 请求中收到 400。 I know it's finding the page because I can make a successful GET request to /LogTransaction.我知道它正在查找该页面,因为我可以向 /LogTransaction 发出成功的 GET 请求。 It's just post requests that don't work.它只是发布请求不起作用。
I didn't have a CSRF token input in the form.我没有在表单中输入 CSRF 令牌。 You can either put it in manually or use the asp-page helper on the form instead of action.您可以手动放入,也可以在表单上使用 asp-page 帮助程序而不是操作。 That helper automatically puts in the CSRF token input apperently该助手会自动输入 CSRF 令牌输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.