## 在ASP.NET Web API下使用[Serializble]的问题

By default you don't need to use neither [Serializble] nor [DataContract] to work with Web API.

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).

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:

[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; }
}

《在ASP.NET Web API下使用[Serializble]的问题》常伟华 创作。

• 多说评论
• 签名
• 新浪微博
• 默认评论