Updated on Kisan Patel
Problem:
I am using Entity Framework and having a problem with getting parent and child data that causes self-referencing loop. How to stop self-referencing loop in .Net Core Web API?
Solution:
If you are using ASP.NET Core, you can configure Json.NET
to ignore cycles that it finds in the object graph. This is done in the ConfigureServices(...)
method in Startup.cs
.
public void ConfigureServices(IServiceCollection services) { .... services.AddMvc() .AddJsonOptions( options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore ) .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }