【发布时间】:2019-05-31 14:41:36
【问题描述】:
我用空的用户名和密码调用注册方法。所以我收到了这个结果:
{
"errors": {
"Password": [
"The Password field is required.",
"Password length is between 4 and 8."
],
"Username": [
"The Username field is required."
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "0HLJIO56EGJEV:00000001"
}
我的 Dto:
public class UserForRegisterDto
{
[Required]
public string Username { get; set; }
[Required]
[StringLength(8, MinimumLength = 4, ErrorMessage = "Password length is between 4 and 8.")]
public string Password { get; set; }
}
我只想从响应中获取错误属性,我该怎么办?
【问题讨论】:
-
您在哪里/如何验证故事?您能否更新您的问题以显示这一点。我使用 ModelState.IsValid 来检查模型状态,如果它无效,您可以只提取错误并在响应中返回。
标签: c# asp.net-core asp.net-core-2.2