That's translation of the answer from here: https://stackoverflow.com/a/18223985/1101913 ♪And the one in turn is reprinted from here: https://docs.microsoft.com/en-us/archive/blogs/hongyes/loop-reference-handling-in-web-api ♪Mode 1: Global neglect of cyclical referencesJSON. NET provides an opportunity to ignore cyclical references. For this, put the next code in the file. WebApiConfig.cs:config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
This way will force a seriesr to ignore the chains of reference that lead to cycling. But he has minus:Cyclical links lostIt only works with JSON. NETIt is not possible to control the level of investment in which the value chain is defined as cyclicalTo use this method in the usual ASP. NET project, add the above line to Global.asax.cs, but first add it:var config = GlobalConfiguration.Configuration;
To use this method in Project Net Core, change Startup.cs in:var mvc = services.AddMvc(options =>
{
...
})
.AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
Mode 2: Global maintenance of cyclical referencesThe second one looks like the first. Just change the code so:config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Serialize;
config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling
= Newtonsoft.Json.PreserveReferencesHandling.Objects;
Once this method is applied, the data structure will be changed to:[
{
"$id":"1",
"Category":{
"$id":"2",
"Products":[
{
"$id":"3",
"Category":{
"$ref":"2"
},
"Id":2,
"Name":"Yogurt"
},
{
"$ref":"1"
}
],
"Id":1,
"Name":"Diary"
},
"Id":1,
"Name":"Whole Milk"
},
{
"$ref":"3"
}
]
The keywords of $Id and $ref allow all references to be kept flat. But the client code must understand the structure of the JSON object. This decision also applies only to the JSON series. NET.Mode 3: Ignoring or maintaining cyclical references through attributesThis method is to add to series class attribu attributes to control the conduct of a series of class or characteristic.To ignore the properties:public class Category
{
public int Id { get; set; }
public string Name { get; set; }
[JsonIgnore]
[IgnoreDataMember]
public virtual ICollection<Product> Products { get; set; }
}
The JsonIgnore attribute is for JSON.NET, and IgnoreDataMember for XmlDCSerializer.To maintain the reference:// Fix 3
[JsonObject(IsReference = true)]
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
// Fix 3
//[JsonIgnore]
//[IgnoreDataMember]
public virtual ICollection<Product> Products { get; set; }
}
[DataContract(IsReference = true)]
public class Product
{
[Key]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public virtual Category Category { get; set; }
}
Atribut JsonObject(IsReference = true)] for JSON. NET, a [DataContract](IsReference = true)] for XmlDCSerializer. Call attention to: DataContract You need to add to the class. DataMember to the properties you want to show.Attributes can be used as a JSON satellite. NET, as well as XML server, and provide more space to manage the show.