k__BackingField 在 C# 中删除(通过 Swashbuckle/Swagger 看到)

k__BackingField remove in C# (seen via Swashbuckle / Swagger)(k__BackingField 在 C# 中删除(通过 Swashbuckle/Swagger 看到))
本文介绍了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 看到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)