• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
简单不先于复杂,而是在复杂之后
博客园    首页    新随笔    联系   管理    订阅  订阅
在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; }
}

 

原文链接:http://stackoverflow.com/questions/12334382/net-webapi-serialization-k-backingfield-nastiness

posted on 2013-04-07 19:27  sharpCode  阅读(442)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3