这是带有 ASP.NET 身份注册页面 (.NET 8) 的 ASP.NET Razor 页面项目。 电子邮件检查 (PageRemote) 工作正常,但前提是 Email 属性不是 Input 类的成员(由 ASP.NET Identity 自动生成)
[BindProperty]
[Required]
[EmailAddress]
[Display(Name = "Email")]
[PageRemote(
ErrorMessage = "This email is already registered",
AdditionalFields = "__RequestVerificationToken",
HttpMethod = "post",
PageHandler = "CheckEmail"
)]
public string? Email { get; set; }
public JsonResult OnPostCheckEmail()
{
var valid = !_dbcontext.Accounts.ToList().Exists(p => p.Email.Equals(Email, StringComparison.CurrentCultureIgnoreCase));
return new JsonResult(valid);
}
一旦将此构造放入输入类中,一切都会停止工作。
[BindProperty]
public InputModel Input { get; set; }
public class InputModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
[PageRemote(
ErrorMessage = "This email is already registered",
AdditionalFields = "__RequestVerificationToken",
HttpMethod = "post",
PageHandler = "CheckEmail"
)]
public string? Email { get; set; }
}
public JsonResult OnPostCheckEmail()
{
var valid = !_dbcontext.Accounts.ToList().Exists(p => p.Email.Equals(Input.Email, StringComparison.CurrentCultureIgnoreCase));
return new JsonResult(valid);
}
我做错了什么?)
当您使用标签助手生成嵌套类属性的输入时,
name
属性将生成为NestedClassName.PropertyName
AdditionalFields
属性中列出的所有字段在发布时都会以嵌套类名称作为前缀,因此,在您的情况下,请求验证令牌将发布为 Input.__RequestVerificationToken
。因此,找不到请求验证令牌本身,并且请求验证失败,导致 400 状态代码。
向 PageModel 添加一个单独的属性 -
Email
- 用于远程验证,如您的工作示例中所示,并在成功验证后,将值分配给嵌套属性。或者编写您自己的 JavaScript 来执行远程验证。