By default you don't need to use neither [Serializble]
nor [DataContract]
to work with Web API.
默认情况下ASP.NET Web API 不需要[Serializble]
或者[DataContract]标记
Just leave your model as is, and Web API would serialize all the public properties for you.
ASP.NET Web API会自动帮我们序列化所有Public属性
Only if you want to have more control about what's included, you then decorate your class with [DataContract]
and the properties to be included with [DataMember]
(because both DCS and JSON.NET respsect these attributes).
如果你想控制只需要哪些属性需要序列化,你可以用[DataContract],
标记。[DataMember]
If for some reason, you need the [Serializble]
on your class (i.e. you are serializing it into a memory stream for some reason, doing deep copies etc), then you have to use both attributes in conjunction to prevent the backing field names:
如果你因为某些原因,必须用[Serializble]
标记(比如将数据保存在Couchbase),那么,你应该同时用[Serializable]和 [DataContract]去控制。
[Serializable] [DataContract] public class Error { [DataMember] public string Status { get; set; } [DataMember] public string Message { get; set; } [DataMember] public string ErrorReferenceCode { get; set; } [DataMember] public List<FriendlyError> Errors { get; set; } }
原文链接:http://stackoverflow.com/questions/12334382/net-webapi-serialization-k-backingfield-nastiness
请发表评论