I'm building some small booking app and I'm getting this error all the time.
I solved it with: config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
but now I'm getting answer like this:
{
"ApartmentId": 1,
"Building": {
"BuildingId": 1,
"Apartments": [
{
"ApartmentId": 2,
}
]
},
}
For class building everything works fine. It's only going "to far" for the apartment. I tried solutions from this topic but it didn't work: https://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type#=
Here are my classes:
public class Apartment
{
public int ApartmentId { get; set; }
public Building Building { get; set; }
}
public class Building
{
public int BuildingId { get; set; }
public List<Apartment> Apartments { get; set; }
}
The question is, what am I missing? How to get rid of listing of apartments for the second time?