我创建了一个非常简单的Web api,它具有如下模型类:
public class Person
{
[JsonRequired]
public int? Age { get; set; }
}
以及以下控制器消息:
[HttpPost]
public void Post([FromBody] Person person)
{
}
当我通过以下json请求进行调用时:
{
"age": "a"
}
我得到:
{
"errors": {
"": [
"Required property 'age' not found in JSON. Path '',line 3,position 1."
],"age": [
"Could not convert string to integer: a. Path 'age',line 2,position 11."
]
},"title": "One or more validation errors occurred.","status": 400,"traceId": "80000007-0003-ff00-b63f-84710c7967bb"
}
我不明白为什么我在这里得到两个错误值,特别是因为一个错误值显然不真实...有什么想法吗?我不想更改为[Required],因为您丢失了解释错误发生在哪个行号上的漂亮错误。
谢谢