【发布时间】:2016-06-18 02:50:40
【问题描述】:
当请求 Json 包含重复键时,我需要从 ASP.NET Web API Post 请求返回 400 错误。
例如,如果请求是
{
"key1": "value1",
"key2": 1000,
"key2": 2000,
"key3": "value3"
}
那么我希望由于有两个“key2”键而引发错误。
我的控制器方法看起来像
[HttpPost]
public IHttpActionResult PostMethod([FromBody]RequestModel request)
{
.....
}
和我的 RequestModel 模型一样
public class RequestModel
{
[Required]
public string Key1 {get; set; }
[Required]
public int Key2 {get; set; }
public string Key3 {get; set; }
}
在上面的示例中,Json 序列化程序似乎很乐意接受请求并使用 2000 填充 Key2,或者任何密钥的最后一个实例。
我想我需要做一些涉及 JsonSerializerSettings 类的事情,或者实现一个自定义 JsonConverter,但是我不确定如何继续。
【问题讨论】:
标签: c# json asp.net-web-api json.net deserialization