ASP.NET Web API and [Serializable] class

后端 未结 1 1252
夕颜
夕颜 2020-12-01 18:46

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

[{\         


        
相关标签:
1条回答
  • 2020-12-01 19:22

    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.

    0 讨论(0)
提交回复
热议问题