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

相关推荐
小先生8128 天前
.NET Core后台任务队列
.net·.netcore
MoFe18 天前
【.net core】【watercloud】动态数据转换为静态表格,或者表格数据返回需要后处理
.netcore
吹牛不交税14 天前
.netcore项目部署在ubuntu22.04虚拟机的docker中的过程记录
docker·容器·.netcore
weixin_4219947817 天前
基于 .NET 9.0 的高性能轻量级令牌桶限流服务
.net·.netcore·令牌桶
weixin_4219947817 天前
MVC 模式初探
mvc·.net·.netcore
weixin_4219947819 天前
互联网与 Web 应用简介
.net·.netcore
全栈小521 天前
【C#】合理使用DeepSeek相关AI应用为我们提供强有力的开发工具,在.net core 6.0框架下使用JsonNode动态解析json字符串,如何正确使用单问号和双问号做好空值处理
人工智能·c#·json·.netcore·deepseek
时光追逐者22 天前
C#/.NET/.NET Core优秀项目和框架2026年1月简报
c#·.net·.netcore
大尚来也24 天前
双库协同,各取所长:.NET Core 中 PostgreSQL 与 SQLite 的优雅融合实战
postgresql·sqlite·.netcore
吹牛不交税25 天前
admin.net-v2 框架使用笔记-netcore8.0/10.0版
vue.js·.netcore