Self referencing loop detected in ASP.NET Core [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

This question already has an answer here:

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.

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

How can we fix this in ASP.NET Core?

回答1:

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(         options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])     ); }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!