k__BackingField 在 C# 中删除(通过 Swashbuckle/Swagger 看到) [英] k__BackingField remove in C# (seen via Swashbuckle / Swagger)

查看:36
本文介绍了k__BackingField 在 C# 中删除(通过 Swashbuckle/Swagger 看到)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 ASP.NET webapi 项目中使用所有默认设置的 Swashbuckle 5.它序列化我的方法的输出,以便向我显示回复的模式.我收到的文档如下所示:

I am using Swashbuckle 5 in my ASP.NET webapi project with all default settings. It serializes my method's output in order to show me schema of the reply. I am getting documentation that looks like this:

 Response Class (Status 200)
 Model  Model Schema
 [
   {
    "<Key>k__BackingField": "string",
    "<Value>k__BackingField": "string",
    "<Id>k__BackingField": 0
  }
]

这是通过以下 C# 代码生成的

This is generated by following C# code

    /// <summary>
    ///     Fetches all system configuration items
    /// </summary>
    /// <returns>List of <see cref="SystemConfigurationDto" /> items</returns>
    public IList<SystemConfigurationDto> GetAllSystemConfigurationItems()
    {
        var result = CommandProcessor.ProcessCommand(new SystemConfigurationQueryCommand()) as SystemConfigurationQueryCommandResponse;

        return result.Results.ToList();
    }

其中 result.Results 基本上是一个标准的对象列表,每个对象都包含这些键/值/id 字段.我在这里读过 https://conficient.wordpress.com/2014/05/22/getting-rid-of-k__backingfield-in-serialization/ [serializable] 属性可能会影响这一点,但如果可能的话,我不愿意摆脱该属性.有什么方法可以调整这个序列化工件吗?

where result.Results is basically a standard List of objects, each containing these key/value/id fields. I have read here https://conficient.wordpress.com/2014/05/22/getting-rid-of-k__backingfield-in-serialization/ that [serializable] attribute might affect this but I am not willing to get rid of that attribute, if possible. Is there any recipe to adjust this serialization artifact?

推荐答案

将此添加到 WebApiConfig.cs:

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
    new DefaultContractResolver { IgnoreSerializableAttribute = true };

这解决了标有 [Serializable] 的类的问题.即使没有类具有该属性,我也会遇到间歇性问题,因此我现在总是使用此设置.

This resolves the issue for classes marked with [Serializable]. I also have intermittent problems even if no classes have that attribute, so I always use this setting now.

这篇关于k__BackingField 在 C# 中删除(通过 Swashbuckle/Swagger 看到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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