.net core api在返回json数据时,出现错误,转json不断shi循环。

1,错误信息:

A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Data.User.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users

翻译后的意思:

检测到可能的对象循环。这要么是由于存在循环,要么是因为对象深度超过了允许的最大深度 32。请考虑在 JsonSerializerOptions 中使用 ReferenceHandler.Preserve 来支持循环。路径:$.Data.User.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users.Company.Users

2,解决方法:

在添加控制器后面加AddJsonOptions()方法,参考下面代码:

cs 复制代码
// 2. 添加控制器
builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
    options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});

谢谢观看,不是在改Bug路上,就是制造bug缔造者!

相关推荐
淡海水1 天前
08-02-优化-LINQ与函数式编程的GC代价
unity·c#·.netcore·linq·gc
淡海水2 天前
07-06-源码-.NET Core GC的极致优化技术
unity·架构·c#·.netcore·gc·1024程序员节
淡海水3 天前
07-02-源码-.NET Core GC分配路径
算法·unity·c#·.netcore·gc·1024程序员节
淡海水3 天前
07-04-源码-.NET Core GC清扫与压缩
unity·架构·c#·.netcore·gc·1024程序员节
1-1=04 天前
.NET Core 本地化 (L10n)
.net·.netcore
1-1=04 天前
.NET Core 健康检查
.net·.netcore·1024程序员节
淡海水4 天前
07-03-源码-.NET Core GC标记与扫描
unity·c#·.netcore·gc
csdn_aspnet4 天前
.NETCore SqlSuagr 根据 mysql 数据表按顺序生成简单编号
mysql·.netcore·sqlsugar·1024程序员节·sequence·.net8·编号
淡海水5 天前
07-01-源码-.NET Core GC总览与架构
unity·架构·c#·.netcore·gc
csdn_aspnet5 天前
在 .NET Core 中使用 Redis
redis·.netcore