在C#中删除k__BackingField(通过Swashbuckle/Swagger查看) [英] k__BackingField remove in C# (seen via 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.
这篇关于在C#中删除k__BackingField(通过Swashbuckle/Swagger查看)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!