【发布时间】:2020-11-04 23:28:06
【问题描述】:
在我的 ASP.NET Core Web API 中,我使用数据注释进行模型验证。这是一个具有这些注释的类:
class CreateUserRequest {
[Required, RegularExpression("[a-zA-Z0-9\-_]+")]
public string Name { get; set; }
[Required, StringLength(20, MinimumLength = 6)]
public string Password { get; set; }
}
如果客户端尝试使用太短的密码创建用户,我会收到以下响应正文:
{
"errors": {
"password":[
"The field password must be a string with a minimum length of 6 and a maximum length of 20."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId":"|a301aaae-43dd49e731beb073."
}
我希望从 API 返回的所有错误都具有一致的结构,以便客户端更轻松地处理错误。是否有一个类可以用来为其他错误情况获取相同的结构?
【问题讨论】:
标签: asp.net-core