json - .net core 3 在 AddJsonOptions 中没有 ReferenceLoopHandling

标签 json asp.net-core

我的 csproject 文件指示:<TargetFramework>netcoreapp3.0</TargetFramework>
在我的启动中,我使用以下内容:

 services.AddMvc(x => x.Filters.AddService<TransactionFilter>())
        .AddJsonOptions(options => options.JsonSerializerOptions... )

但是,ReferenceLoopHandling 在 options.JsonSerializerOptions 中不可用.

enter image description here
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentNHibernate" Version="2.1.2" />
    <PackageReference Include="FullContact.Contacts.API" Version="1.0.3" />
    <PackageReference Include="Google.Cloud.Storage.V1" Version="2.3.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Cors" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.5.0" />
    <PackageReference Include="MySql.Data" Version="8.0.17" />
    <PackageReference Include="piplclient" Version="5.0.9" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.5.0" />
  </ItemGroup>

</Project>

最佳答案

作为工作的一部分到improve the ASP.NET Core shared framework , Json.NET已从 ASP.NET Core 共享框架中删除。如果您的应用程序使用 Newtonsoft.Json 特定功能(例如 JsonPatch 或转换器),或者如果它 formats,则您的应用程序可能需要此引用。 Newtonsoft.Json 特定类型。

在 ASP.NET Core 3.0 项目中使用 Json.NET:

添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的包引用.

更新 Startup.ConfigureServices调用 AddNewtonsoftJson .

services.AddMvc()
.AddNewtonsoftJson();

这将设置 MVC 并将其配置为使用 Json.NET 而不是新的 API。并且 AddNewtonsoftJson 方法有一个重载,允许您像在 ASP.NET Core 2.x 中使用 AddJsonOptions 一样配置 Json.NET 选项。
services.AddMvc()
.AddNewtonsoftJson(options =>
{
    options.SerializerSettings = new JsonSerializerSettings() { … };
});

引用:

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio#jsonnet-support

https://stackoverflow.com/a/55666898/10201850

关于json - .net core 3 在 AddJsonOptions 中没有 ReferenceLoopHandling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58006152/

相关文章:

javascript - 如何检查Json中元素的顺序没有改变

ios - 在 SWIFT 3 中从网页中读取 JSON 格式

javascript - 无法使用 Angular JS 获取纯文本格式的 JSON 数据

c# - .NET Core - 读取 JSON 文件的转换日期

asp.net-core - HTTP 错误 500.30 - ASP.NET Core 2.2 中的 ANCM 进程内启动失败错误

java - 春假服务

python - 从 pandas DataFrame 中的列中提取 JSON 数据

c# - ASP.Net vNext App_Data 文件夹

asp.net-web-api - 在 ASP.NET Core RC2 中使用 HttpConfiguration

asp.net - 创建 .NET Core 1.1 Web 应用程序时出现问题