繁体   English   中英

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

[英]k__BackingField remove in C# (seen via Swashbuckle / Swagger)

我在ASP.NET webapi项目中使用Swashbuckle 5并使用所有默认设置。 它序列化我的方法的输出,以显示回复的模式。 我收到的文档看起来像这样:

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

这是通过遵循C#代码生成的

    /// <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 ]属性可能会影响这个,但我不愿意摆脱它属性,如果可能的话。 是否有任何配方来调整此序列化工件?

将其添加到WebApiConfig.cs

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM