我知道这个问题在其他框架和语言中已经被问了好几次,但我想知道如何在ASP.NET Core 2.1中一旦出现第一个错误就停止模型验证。
以上代码是API模型验证,在进行Patch请求时,我收到以下响应:
当模型验证确认电子邮件在数据库中不存在时,我不希望继续进行验证,只需返回该错误并停止验证即可。
[Required(ErrorMessage = Email.requiredErrorMessage)]
[DataType(DataType.EmailAddress, ErrorMessage = Email.formatErrorMessage)]
[StringLength(Email.maximumLength, MinimumLength = Email.minimumLength, ErrorMessage = Email.rangeErrorMessage)]
[EmailExists(ErrorMessage = Email.doesNotExistsMessage)]
public string email { get; set; }
[Required(ErrorMessage = ConfirmationCode.requiredErrorMessage)]
[RegularExpression(ConfirmationCode.regularExpression, ErrorMessage = ConfirmationCode.formatErrorMessage)]
public string confirmationCode { get; set; }
以上代码是API模型验证,在进行Patch请求时,我收到以下响应:
"email":[
"This email does not exist in database"
],
"confirmationCode":[
"Confirmation code format is invalid"
]
}
当模型验证确认电子邮件在数据库中不存在时,我不希望继续进行验证,只需返回该错误并停止验证即可。