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

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

Response Class (Status 200) Model Model Schema [ { "k__BackingField": "string", "k__BackingField": "string", "k__BackingField": 0 } ] 

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

  ///  /// Fetches all system configuration items ///  /// List of  items public IList 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]类的问题。 即使没有类具有该属性,我也有间歇性问题,因此我现在总是使用此设置。