在Json.net设置IgnoreSerializableAttribute全球 [英] Setting IgnoreSerializableAttribute Globally in Json.net

查看:341
本文介绍了在Json.net设置IgnoreSerializableAttribute全球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET的WebAPI(发布候选)项目中,我耗费了标有[Serializable]属性数的DTO。这些DTO的是我无法控制的,所以我不能够进行任何方式。当我回到任何一个从get方法生成的JSON包含这样一群k__BackingFields的:

I'm working on a ASP.NET WebApi (Release Candidate) project where I'm consuming several DTOs that are marked with the [Serializable] attribute. These DTOs are outside of my control so I'm not able to modify them in any way. When I return any of these from a get method the resulting JSON contains a bunch of k__BackingFields like this:

<Name>k__BackingField=Bobby
<DateCreated>k__BackingField=2012-06-19T12:35:18.6762652-05:00

根据我做这个搜索似乎是一个问题JSON.NET的IgnoreSerializableAttribute 设置,要解决我的问题我只需要为文章建议在全球范围内设置。我如何在ASP.NET的Web API项目在全球范围更改此设置?

Based on the searching I've done this seems like a problem with JSON.NET's IgnoreSerializableAttribute setting and to resolve my issue I just need to set it globally as the article suggests. How do I change this setting globally in a ASP.NET Web api project?

推荐答案

我找到简单的方法来在名称摆脱k__BackingField的。

I found easy way to get rid of k__BackingField in the names.

该片段应在Global.asax.cs中的的Application_Start()的地方:

This fragment should be somewhere in the Application_Start() in Global.asax.cs:

JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings();
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = jSettings;

貌似默认设置照顾它。

Looks like the default setting takes care of it.

这篇关于在Json.net设置IgnoreSerializableAttribute全球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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