6
많은 시간 내가 그 개발자는 Asp.net 웹 API 모델에 대한 DataContract 및 DataMember 특성을 사용하는 것을 봤어?Asp.net 웹 API로 작업하는 동안 DataContract 및 DataMember 특성을 사용하는 데 이점이 있습니까?
차이점과 모범 사례는 무엇입니까?
많은 시간 내가 그 개발자는 Asp.net 웹 API 모델에 대한 DataContract 및 DataMember 특성을 사용하는 것을 봤어?Asp.net 웹 API로 작업하는 동안 DataContract 및 DataMember 특성을 사용하는 데 이점이 있습니까?
차이점과 모범 사례는 무엇입니까?
DataContract
을 사용하는 주된 이점은 XmlMediaTypeFormatter
및 JsonMediaTypeFormatter
에 대한 몇 가지 일반적인 일련 화 힌트에 대해 중복 된 특성을 피할 수 있다는 것입니다. 나는. 당신은 직렬화 될 모델의 특정 속성을 옵트 인/옵트 아웃 (opt-in/opt-out)하거나 속성의 이름을 바꾸고 양쪽 포맷터를 존중하도록 설정할 수 있습니다. 예를 들어
는 :
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;}
}
:
[DataContract]
public class Sample {
[DataMember]
public string PropOne {get;set;}
public string PropTwo {get;set;}
[DataMember(Name="NewName")]
public string PropThree {get; set;}
}
에 상당