【问题标题】:.NET Core 2.1 JSon.NET Formatters.NET Core 2.1 JSON.NET 格式化程序
【发布时间】:2018-12-16 15:39:25
【问题描述】:

我有一个问题,在使用自动属性 ​​Json.Net 的类中将 k__BackingField 添加到属性名称。

我查看了各种建议(即添加 [JsonObject]),但没有一个适合我。

我发现一种推荐的解决方案是:

((Newtonsoft.Json.Serialization.DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;

(https://stackoverflow.com/a/36910449/460084)

但是我不知道在 .NET Core 2.1 中的何处或如何执行此操作?这在 Startup.cs 中吗?怎么样?

我什至不确定这会有所帮助,因为我的班级没有 [Serializable] 开头。

有什么帮助吗?真的没有简单的方法来更改 Json.Net 的默认值以使用 .NET Core 2.1 中没有 k__BackingField 的属性名称

【问题讨论】:

    标签: asp.net-core json.net .net-core asp.net-core-2.1


    【解决方案1】:

    将其添加到 ConfigureServices 方法中的 Startup.cs:

    // This method gets called by the runtime. Use this method to add services to the container.
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services
            .AddMvc()
            .AddJsonOptions(options => (options.SerializerSettings.ContractResolver as DefaultContractResolver).IgnoreSerializableAttribute = true);
    
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 2013-11-07
      • 2021-03-15
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 2020-01-21
      • 2017-09-11
      相关资源
      最近更新 更多