.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缔造者!

相关推荐
鸠摩智首席音效师14 天前
如何在 macOS 上安装 .NET Core ?
macos·.netcore
宝桥南山18 天前
Microsoft Agent Framework(MAF) - 如何将workflow或者A2A client转换成一个AI Agent
microsoft·ai·微软·aigc·.net·.netcore
滴滴答答哒1 个月前
.NET Core 基于 AOP + Polly 实现数据库死锁自动重试
数据库·.netcore·sqlsugar
.NET修仙日记1 个月前
.NET EFCore批量插入性能优化实战:30秒 → 0.5秒
性能优化·c#·.net·.netcore·微软技术·efcore·踩坑实录
Kimhill张1 个月前
.net core8 WPF 依赖注入(DI)
wpf·.netcore
wangl_921 个月前
C# / .NET 在工业环境中的优势
开发语言·c#·.net·.netcore·.net core·visual studio
豆豆1 个月前
信创环境下CMS国产化适配实践:以.NET Core路线为例的技术验证
.netcore·cms·信创·国产化·建站系统·内容管理系统·网站管理系统
时光追逐者1 个月前
C#/.NET/.NET Core技术前沿周刊 | 第 70 期(2026年5.01-5.10)
c#·.net·.netcore
van久1 个月前
Day20:AutoMapper 对象映射
.netcore
van久1 个月前
Day23 登录 + 颁发 Token(DDD 四层架构 + 企业标准)
.netcore