0
我有一个简单的模型:asp.net核心 - 的ModelState错误请求不一致的行为
public class Item : Entity<int>
{
[Required]
public string Name { get; set; }
[Required]
public int Cost { get; set; }
}
内部控制器:
[HttpPost]
public async Task<IActionResult> Create([FromBody] Item item)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}
repository.Create(item);
await repository.SaveAsync();
return Created($"/api/v1/items/{item.Id}", new { message = "Item was created successfully!" });
}
现在,对于以下三种不正确采样输入我得到以下答复:
样品#1:
POST http://localhost:5000/api/v1/items
content-type: application/json
{
"name": "sample1",
"cost": $100000
}
响应:
{
"cost": [
"The input was not valid."
]
}
样品#2:
POST http://localhost:5000/api/v1/items
content-type: application/json
{
"name": "sample2",
"cost": "10000000000000000000000000"
}
响应:
{
"cost": [
"The input was not valid."
]
}
样品#3:这一次似乎是一个错误。空键出现(的int,而不是超出范围的错误)
POST http://localhost:5000/api/v1/items
content-type: application/json
{
"name": "sample3",
"cost": 10000000000000000000000000
}
响应:
{
"": [
"The input was not valid."
],
"cost": [
"The input was not valid."
]
}
编辑: 添加在github跟踪票 - https://github.com/aspnet/Mvc/issues/5672
我知道,但为什么错误显示没有密钥的额外属性 –
想念你的问题,对不起。我的猜测是,它找到最后一个开头''',并认为它开始一个新的值。由于没有定义名称,它将它输出为空和无效。它似乎是一个错误,因为它不应该考虑这个'''for一个新的价值,而不是“成本”的一部分,就像它在“之前”那样。我会在github https://github.com/aspnet/Home上报告它 – Mats391