在ASP.NET Core中检测到自引用循环 [英] Self referencing loop detected in ASP.NET Core

查看:153
本文介绍了在ASP.NET Core中检测到自引用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用ASP.NET Core Newsoft JSON.NET序列化某些域对象时,它会引发异常,因为它正在检测自引用循环.

When I try to serialize some domain objects using ASP.NET Core Newsoft JSON.NET it is throwing an exception because it is detecting a self referencing loop.

在ASP.NET 4中,我们通常通过以下方式对其进行全局修复: JSON.NET错误检测到类型的自引用循环

In ASP.NET 4 we used to fix it globally this way: JSON.NET Error Self referencing loop detected for type

我们如何在ASP.NET Core中解决此问题?

How can we fix this in ASP.NET Core?

推荐答案

与ASP.NET Core(以前称为Asp.Net 5)相比,ASP.NET 4中处理自引用循环的方式没有区别.您在帖子中引用的问题中概述的原则仍然适用.但是,鉴于配置和引导应用程序的新方法,在ASP.NET Core中设置此属性显然稍有不同:

There is no difference in the way self-referencing loops are handled in ASP.NET 4 compared to ASP.NET Core (previously Asp.Net 5). The principles outlined in the question you referenced in your post still apply. However, setting this property in ASP.NET Core is obviously slightly different, given the new method of configuring and bootstrapping the app:

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"])
    );
}

这篇关于在ASP.NET Core中检测到自引用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆