Razor 页面表单不适用于 post 请求
Razor page form is not working with post request
我正在学习 .NET,我试图从没有标签助手的简单表单传递数据,但该表单不起作用,我也不知道为什么。很简单,一个索引文件
@page
@model WebApplication1.Pages.IndexModel
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<form class="form-container" method="post">
<input type="text" class="input-item" placeholder="Your First Name" name="FirstName">
<input type="text" class="input-item" placeholder="Your Last Name" name="LastName">
<input type="submit" value="Submit">
</form>
它的型号
namespace WebApplication1.Pages
{
public class IndexModel : PageModel
{
[BindProperty]
public InputModel Input { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
return RedirectToPage("Success");
}
public class InputModel
{
public string FirstName { get; set; }
public string LastName{ get; set; }
}
}
}
当它点击 post 方法时,它应该重定向到成功页面。
Razor Pages 中的 400 状态代码通常是 failed request verification check 的结果。当方法设置为 post 时,表单标签助手在隐藏字段中生成验证码。如果您禁用了标签助手(或根本没有启用它们),您可以通过包含
来生成令牌
@Html.AntiForgeryToken()
在表单中。或者,您可以通过将 IgnoreAntiforgeryToken
添加到 PageModel class:
来禁用请求验证
[IgnoreAntiforgeryToken(Order = 1001)]
public class IndexModel : PageModel
<form method="post" class="login-form">
<div class="form-group">
<div class="icon d-flex align-items-center justify-content-center"><span
class="fa fa-user"></span></div>
<input type="text" class="form-control" asp-for="FirstName" placeholder="FirstName"
required>
</div>
<div class="form-group">
<div class="icon d-flex align-items-center justify-content-center"><span
class="fa fa-lock"></span></div>
<input type="password" class="form-control" asp-for="LastName"
placeholder="LastName" required>
</div>
<div class="form-group">
<button type="submit" id="submitBtn"
class="btn form-control btn-primary rounded submit px-3">Go!</button>
</div>
</form>
//Login.cshtml.cs
[BindProperty]
public string FirstName { get; set; }
[BindProperty]
public string LastName { get; set; }
public async Task<IActionResult> OnPostAsync()
{
LoginRequest request = new LoginRequest
{
FirstName = FirstName,
LastName = LastName,
};
.
.
.
if (..)
{
return RedirectToPage("./Home");
}
return Page();
}
我正在学习 .NET,我试图从没有标签助手的简单表单传递数据,但该表单不起作用,我也不知道为什么。很简单,一个索引文件
@page
@model WebApplication1.Pages.IndexModel
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<form class="form-container" method="post">
<input type="text" class="input-item" placeholder="Your First Name" name="FirstName">
<input type="text" class="input-item" placeholder="Your Last Name" name="LastName">
<input type="submit" value="Submit">
</form>
它的型号
namespace WebApplication1.Pages
{
public class IndexModel : PageModel
{
[BindProperty]
public InputModel Input { get; set; }
public void OnGet()
{
}
public IActionResult OnPost()
{
return RedirectToPage("Success");
}
public class InputModel
{
public string FirstName { get; set; }
public string LastName{ get; set; }
}
}
}
当它点击 post 方法时,它应该重定向到成功页面。
Razor Pages 中的 400 状态代码通常是 failed request verification check 的结果。当方法设置为 post 时,表单标签助手在隐藏字段中生成验证码。如果您禁用了标签助手(或根本没有启用它们),您可以通过包含
来生成令牌@Html.AntiForgeryToken()
在表单中。或者,您可以通过将 IgnoreAntiforgeryToken
添加到 PageModel class:
[IgnoreAntiforgeryToken(Order = 1001)]
public class IndexModel : PageModel
<form method="post" class="login-form">
<div class="form-group">
<div class="icon d-flex align-items-center justify-content-center"><span
class="fa fa-user"></span></div>
<input type="text" class="form-control" asp-for="FirstName" placeholder="FirstName"
required>
</div>
<div class="form-group">
<div class="icon d-flex align-items-center justify-content-center"><span
class="fa fa-lock"></span></div>
<input type="password" class="form-control" asp-for="LastName"
placeholder="LastName" required>
</div>
<div class="form-group">
<button type="submit" id="submitBtn"
class="btn form-control btn-primary rounded submit px-3">Go!</button>
</div>
</form>
//Login.cshtml.cs
[BindProperty]
public string FirstName { get; set; }
[BindProperty]
public string LastName { get; set; }
public async Task<IActionResult> OnPostAsync()
{
LoginRequest request = new LoginRequest
{
FirstName = FirstName,
LastName = LastName,
};
.
.
.
if (..)
{
return RedirectToPage("./Home");
}
return Page();
}