这个问题在这里已经有了答案:
JSON.NET Error Self referencing loop detected for type
(26 个回答)
3年前关闭。
当我尝试使用 ASP.NET Core Newsoft JSON.NET 序列化某些域对象时,它抛出异常,因为它正在检测自引用循环。
在 ASP.NET 4 中,我们曾经通过这种方式全局修复它:
JSON.NET Error Self referencing loop detected for type
我们如何在 ASP.NET Core 中解决这个问题?
最佳答案
与 ASP.NET Core(以前称为 Asp.Net 5)相比,ASP.NET 4 中处理自引用循环的方式没有区别。您在帖子中引用的问题中概述的原则仍然适用。但是,鉴于配置和引导应用程序的新方法,在 ASP.NET Core 中设置此属性显然略有不同:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddEntityFramework().AddSqlServer().AddDbContext<IvoryPacketDbContext>(
options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
);
}
关于json.net - 在 ASP.NET Core 中检测到自引用循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34753498/