【发布时间】:2017-01-10 06:25:36
【问题描述】:
我有一个简单的模型:
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 out of range 错误)
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
【问题讨论】:
标签: c# asp.net validation asp.net-core