.NET的WebAPI序列化k_BackingField污秽 [英] .NET WebAPI Serialization k_BackingField Nastiness

查看:1361
本文介绍了.NET的WebAPI序列化k_BackingField污秽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我序列如下:

[Serializable]
public class Error
{

    public string Status { get; set; }
    public string Message { get; set; }
    public string ErrorReferenceCode { get; set; }
    public List<FriendlyError> Errors { get; set; }
}

我得到这个恶心的烂摊子:

I get this disgusting mess:

<ErrorRootOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://schemas.datacontract.org/2004/07/Printmee.Api">
<_x003C_Errors_x003E_k__BackingField>
An exception has occurred. Please contact printmee support
</_x003C_Errors_x003E_k__BackingField>
<_x003C_LookupCode_x003E_k__BackingField>988232ec-6bc9-48f3-8116-7ff7c71302dd</_x003C_LookupCode_x003E_k__BackingField>
</ErrorRootOfstring>

怎么办?我怎样才能让这个pretty? JSON响应也包含了k_BackingField

What gives? How can i make this pretty? JSON responses also contain the k_BackingField

推荐答案

默认情况下你不需要使用既不 [Serializable接口] 也不 [DataContract] 与Web API的工作。

By default you don't need to use neither [Serializable] nor [DataContract] to work with Web API.

只要留下您的模型是,和Web API将序列所有的公共属性为您服务。

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

[数据成员] (因为DCS和JSON.NET都respsect这些属性)。

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).

如果由于某种原因,你需要的 [Serializable接口] 你的类(即你是序列化它变成一个内存流出于某种原因,在做深拷贝等),那么你必须使用两种结合属性prevent支持字段名:

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

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

这篇关于.NET的WebAPI序列化k_BackingField污秽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆