Entity framework core 从NetCore 2 Newtonsoft JSON移动到NetCore 3中的新JSON API时添加选项
我最近将我的Entity Framework核心项目从DotNet Core 2.2升级到了3.1 我使用的是newtonsoft json,但我想知道是否仍然需要这两行代码导致错误。以下是这两行:Entity framework core 从NetCore 2 Newtonsoft JSON移动到NetCore 3中的新JSON API时添加选项,entity-framework-core,asp.net-core-3.0,Entity Framework Core,Asp.net Core 3.0,我最近将我的Entity Framework核心项目从DotNet Core 2.2升级到了3.1 我使用的是newtonsoft json,但我想知道是否仍然需要这两行代码导致错误。以下是这两行: services.AddMvc() .AddJsonOptions( options => { options.Ser
services.AddMvc()
.AddJsonOptions(
options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
下面是错误:
“JsonOptions”不包含“SerializerSettings”的定义,也没有可访问的扩展方法
“SerializerSettings”接受类型为“JsonOptions”的第一个参数
可以找到
新的Microsoft JSON库是否有像Newtonsoft JSON那样忽略引用循环和空值的功能?这是
System.Text.JSON
的已知限制,该功能可能在计划于2020年11月发布的.net 5中解决:
参考:及
目前的解决办法是改用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; });