.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-1=09 天前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore
1-1=012 天前
C# IO和序列化
c#·.net·.netcore
宝桥南山13 天前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
1-1=014 天前
C# 特性(Attribute)
c#·.net·.netcore
淡海水15 天前
10-01-互操作-C#与C++边界的内存管理
unity·c#·游戏引擎·.netcore·gc
1-1=015 天前
C# 反射
c#·.net·.netcore
1-1=016 天前
C# 泛型
c#·.net·.netcore
淡海水18 天前
09-01-内存-Unity资源生命周期全景
unity·c#·游戏引擎·.netcore·gc
1-1=018 天前
.NET Core 编译技术
.net·.netcore
1-1=019 天前
.NET Core 文件系统
.net·.netcore·1024程序员节