问题描述
我有一个标有[Serializable接口]的类。当我从Web API返回它的字段名称都是时髦的。
通常情况下,JSON返回的是
[{订单ID:797 ...
在使用
JSON返回[Serializable接口]
[{< ORDERID> k__BackingField:797 ...
我wan't将其标记为可序列化使用BinaryFormatter的缓存。是否有比写一个自定义序列,或使双类,它是不可序列化,写猴子code以铸造两者之间有什么别的办法?
您只需要这一个班轮获得Json.NET再次忽略[Serializable接口]语义:
<$p$p><$c$c>((DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = TRUE;为您提供更好的解决方案可能是摆脱[Serializable接口]总之,停止使用BinaryFormatter的,并使用不同的序列化,而不是做任何缓存,你想做的事,比如Json.NET串行例如
I have a class that is marked with [Serializable]. When i return it from the Web API the field names are all funky.
Normally the JSON returned is
[{"OrderId":797 ...
JSON returned when using [Serializable]
[{"<OrderId>k__BackingField":797 ...
I wan't to mark it serializable to use a BinaryFormatter for caching. Is there any other way than to write a custom serializer or to make a twin class that is not serializable and write monkey code to "cast" between the two?
You just need this one-liner to get Json.NET to ignore the [Serializable] semantics again:
((DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;
A better solution for you might be to get rid of [Serializable] altogether, stop using BinaryFormatter, and use a different serializer instead to do whatever caching you want to do, like the Json.NET serializer for example.
这篇关于的ASP.NET Web API和[Serializable接口]类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!