故如虹,知恩;故如月,知明
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

.net core序列化报错JsonSerializationException: Self referencing loop detected for property

8215人阅读 2020/1/23 22:01 总访问:3841106 评论:0 收藏:0 手机
分类: .NET Core


在framework中可以使用如下代码解决:

context.Configuration.ProxyCreationEnabled = false;

但是在.net core中就不行了


.net core中可以使用两种方法解决,推荐方法2


方法1:

JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
//解决循环引用问题
                jsonSerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                string userstr = JsonConvert.SerializeObject(user, jsonSerializerSettings);
                HttpContext.Session.SetString("userInfo", userstr)


方法2:

查询的时候加上方法AsNoTracking()即可


欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价