gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:35:38 27 4
gpt4 key购买 nike

我在我的 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] 属性可能会影响这一点,但如果可能的话,我不愿意摆脱该属性。有什么recipe可以调整这个序列化神器吗?

最佳答案

将此添加到 WebApiConfig.cs:

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

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

关于c# - k__BackingField 在 C# 中删除(通过 Swashbuckle/Swagger 看到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29701891/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com