【发布时间】:2020-11-04 13:12:26
【问题描述】:
我有一个带有静态和动态验证的 ASP.NET Core REST Api。
当我没有指定一个必需的属性时,中间件会自动生成一个很好的错误信息:
{
"errors": {
"status": [
"Required property 'status' not found in JSON. Path '', line 37, position 1."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|a6b5a076-4b400dadb53f75e7."
}
我也有某种动态验证,我手动填充 ModelState 并返回一个 BadRequest。
简化示例:
ModelState.AddModelError("smh_data.materials.country_of_origin", "Field is required.");
return BadRequest(ModelState);
但在这种情况下,响应如下所示:
{
"smh_data.materials.country_of_origin": [
"Field is required."
]
}
我怎样才能得到相同的响应对象,包括上面的traceId、类型、标题和状态?
【问题讨论】:
-
另见:stackoverflow.com/q/63035066/70345(类似,但不完全相同)。
标签: c# asp.net-core .net-core