[Serializble] in ASP. NET Web APIs

Source: Internet
Author: User

By default you don't need to use neither[Serializble]Nor[DataContract]To work with web API.

ASP. NET Web APIs are not required by default.[Serializble]Or[Datacontract] Mark

Just leave your model as is, and web API wocould serialize all the public properties for you.

ASP. NET web API will automatically help us serialize all public attributes

Only if you want to have more control about what's wrong ded, you then decorate your class[DataContract]And the properties to be encoded[DataMember](Because both DCS and JSON. Net respsect these attributes ).

You can use[DataContract],[DataMember]Mark.

If for some reason, you need[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:

For some reason, you must use[Serializble]Mark (for example, save data in couchbase), then you should use[Serializable] and [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; }}

 

Link: http://stackoverflow.com/questions/12334382/net-webapi-serialization-k-backingfield-nastiness

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.