【问题标题】:Add options when moving from NetCore 2 Newtonsoft JSON to the new JSON API in NetCore 3从 Net Core 2 Newtonsoft JSON 迁移到 Net Core 3 中的新 JSON API 时添加选项
【发布时间】:2020-01-30 11:04:03
【问题描述】:

我最近将我的 Entity Framework Core 项目从 DotNet Core 2.2 升级到 3.1。

我正在使用 newtonsoft json,但我想知道是否还需要导致错误的这两行。这是这两行:

        services.AddMvc()
                .AddJsonOptions(
                    options =>
                    {
                        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                        options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                    }); 

这是错误:

“JsonOptions”不包含“SerializerSettings”的定义,也没有可访问的扩展方法 'SerializerSettings' 接受'JsonOptions' 类型的第一个参数 可以找到

新的 Microsoft JSON 库是否有任何东西会像 Newtonsoft JSON 那样忽略引用循环和空值?

【问题讨论】:

    标签: entity-framework-core asp.net-core-3.0


    【解决方案1】:

    这是 System.Text.Json 的一个已知限制,此功能可能会在 .net 5 中得到解决,计划于 2020 年 11 月发布:

    参考:https://github.com/dotnet/corefx/issues/38579https://github.com/dotnet/corefx/issues/41002

    目前的解决方法是改用 Newtonsoft JSON。在 ASP.NET Core 3.0 MVC 项目中使用Newtonsoft.Json

    • 安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包。
    • 更新Startup.ConfigureServices 以调用AddNewtonsoftJson 并设置设置:

      services.AddMvc()
      .AddNewtonsoftJson(options => {
              options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
              options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
          });
      

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      相关资源
      最近更新 更多