Cru*_*KID 6 asp.net datacontract data-members asp.net-web-api
很多时候,我看到开发人员正在使用DataContract和DataMember属性作为他们的Asp.net Web API模型?
有什么区别和最佳做法?
使用的主要优点DataContract是,您可以避免使用XmlMediaTypeFormatter和的一些常见序列化提示的重复属性JsonMediaTypeFormatter.即,您可以选择加入/选择退出要序列化的模型的特定属性,或重命名属性,并让两个格式化程序都尊重它.
例如:
[DataContract]
public class Sample {
[DataMember]
public string PropOne {get;set;}
public string PropTwo {get;set;}
[DataMember(Name="NewName")]
public string PropThree {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
相当于:
public class Sample {
public string PropOne {get;set;}
[XmlIgnore]
[JsonIgnore]
public string PropTwo {get;set;}
[JsonProperty(PropertyName = "NewName")]
[XmlElement("NewName")]
public string PropThree {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3273 次 |
| 最近记录: |