【发布时间】:2016-03-02 10:43:09
【问题描述】:
我正在尝试实现 ASP.NET MVC 5 WebAPI 补丁。但是 int 值和枚举存在问题。 JSON反序列化“认为”更好地将int转换为long,这是因为在我的int属性中我总是接受0 ...
所以我找到了“Microsoft.AspNet.JsonPatch.JsonPatchDocument”(还有很多其他的)
那么问题是,我的模型中总是返回 null
public async Task<IHttpActionResult> Patch( int id, [FromBody] Microsoft.AspNet.JsonPatch.JsonPatchDocument<Customer> model)
{
//model is allways == null
}
我正在使用 POSTMAN 在 Body>Raw 中以 json 格式发送补丁。标头是 application/json。
我不明白为什么模型为空...我需要在 WebApiConfig 上做些什么?
我仍然尝试实现自定义 JsonConverter,但问题是我有很多枚举类型,我需要为每个枚举创建一个吗?我尝试这样的事情:
public class Int32EnumConverter<T> : JsonConverter
但问题出在 WebApiConfig.cs 你需要实现这个:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Int32EnumConverter<[I NEED HEAR Dynamic Enum Types>>);
有没有人可以帮助我?谢谢!
【问题讨论】:
-
为什么不直接使用开箱即用的 Json.net 而无需注册其他序列化程序?
-
因为 http 补丁中的 JSON.net 在 long 中反序列化 int...
-
我还是看不出问题出在哪里。
-
如果你不知道尝试自己创建这个
-
你能分享你的
Int32EnumConverter<T>吗?
标签: c# asp.net asp.net-mvc asp.net-web-api json.net