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

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

问题描述

我正在开发一个 ASP.NET WebApi (Release Candidate) 项目,我正在使用多个标有 [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天全站免登陆