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; });
浙公网安备 33010602011771号