Newtonsoft.Json循环引用异常解决方案,实体对象无法被Newtonsoft.Json解析,实体存在父子双向导航属性

 

  报错内容:Newtonsoft.Json.JsonSerializationException: Self referencing loop detected with type 'Abp.Organizations.OrganizationUnit'. Path 'result.firstOrganizationUnit[0].parent.parent.parent.children'.
  Self referencing loop detected with type 'Abp.Organizations.OrganizationUnit'

  Path 'result.firstOrganizationUnit[0].parent.parent.parent.children'

  问题原因OrganizationUnit 实体存在父子双向导航属性。

  解决办法Startup.cs 或 ABP 模块的 ConfigureServices
  具体配置如下

// Newtonsoft.Json 配置
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
// 可选:保留引用(前端需特殊处理)
// options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
});

或者
services.AddControllersWithViews(options =>
{
options.Filters.Add(new AbpAutoValidateAntiforgeryTokenAttribute());
})
.AddRazorRuntimeCompilation()
.AddNewtonsoftJson(options => { options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; });

 

posted on 2026-03-11 13:28  醉驾的猫  阅读(12)  评论(0)    收藏  举报

导航