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

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


知识共享许可协议
《在ASP.NET Web API下使用[Serializble]的问题》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     160.76ms