我应该在哪里插上我的自定义DefaultContractResolver JSON.NET? [英] Where should I plug in my custom DefaultContractResolver JSON.NET?

查看:872
本文介绍了我应该在哪里插上我的自定义DefaultContractResolver JSON.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.Net一个的WebAPI 2.0项目中,我实现了自定义DefaultContractResolver,这样我可以控制我的实体如何使用序列化JSON JSON.Net到;但是我不确定我怎么能告诉框架使用个性化的实现。我也想知道如果可以更改为ContractResolver特定的控制器/动作。

I have a WebAPI 2.0 project in ASP.Net in which I implemented a custom DefaultContractResolver so that I can control how my entities are serialized to JSON using JSON.Net; however I am unsure about how can I tell the framework to use my custom implementation. I'd also like to know if its possible to change the ContractResolver for a specific controller / action.

谢谢!

---编辑2014年3月7日
我已经想通了如何通过创建一个新的对象ConfigSettings手动解析对象使用个性化的实施;但是生成的JSON序列化两次(它有额外的\\ r \\ n个字符)。这是因为默认情况下的WebAPI控制器序列化响应造成的,所以我想改变重写该行为...这是正确的方式去?

--- Edit 03/07/2014 I've already figured out how to use my custom implementation by creating a new ConfigSettings object and manually parsing the object; However the resulting JSON is serialized twice (it has extra \r\n characters). This is caused because the webAPI controller by default serializes the response, So i'd like to change to override that behavior... Is this the right way to go?

推荐答案

我刚刚经历盘算,自己出来的痛苦了,我需要一种全请求的作​​品。您可以使用这种方法,只是返回相同的媒体格式。我发现上设置格式化<$c$c>GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver有点靠不住为每个请求的需求,尽管我试图在处理这个类的一个单一实例的每个请求的需求。你可以,但是请尝试设置ContractResolver实例有,某处App_Start code

I just went through the pains of figuring that out myself and I needed one that works per-request. You can use that approach and just return the same media formatter. I found setting a formatter on GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver a bit unreliable for per-request needs, even though I tried handling the per-request needs in one single instance of that class. You can, however try setting your ContractResolver instance there, somewhere in App_Start code

我结束了创建自定义的 JsonMediaTypeFormatter ,检查是否存在已配置ContractResolver的请求,你可以只返回一个相同的解析:

I ended up creating a custom JsonMediaTypeFormatter that checks if there is a configured ContractResolver for the request, you can just return one and the same resolver:

public class DynamicJsonMediaTypeFormatter : JsonMediaTypeFormatter
{ 
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, System.Net.Http.HttpRequestMessage request, System.Net.Http.Headers.MediaTypeHeaderValue mediaType)
{
// shown is getting the current formatter, but you can return an instance you prefer
  var formatter = base.GetPerRequestFormatterInstance(type, request, mediaType) as JsonMediaTypeFormatter; 

// Here I had more code to get the resolver based on request, skipped
  ((JsonMediaTypeFormatter)formatter).SerializerSettings.ContractResolver = <YourContractResolverInstance>;
    return formatter;
}
}

我presume你想通的那部分已经但是你的合同解析器可以覆盖`CreateProperties,有自己的逻辑决定什么JSON的属性将是present,将他们使用的名称(添加的完整性和其他读者的利益):

I presume you've figured that part out already but your contract resolver can override `CreateProperties' and have your own logic decide what json properties will be present and what names will they use (added for completeness and benefit of other readers):

public class DynamicContractResolver : DefaultContractResolver
{
...
 protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization){
  ...
}

这篇关于我应该在哪里插上我的自定义DefaultContractResolver JSON.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆