To solve the problem:
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.
There are at least two options:
- Remove circular references.
- Ignore circular references.
The easy way to fix it is to ignore circular references, to do this we need to add this piece of code to services configuration:
services.AddControllers().AddJsonOptions(x =>
x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
Don’t forget to check my previous posts
Solving “A possible object cycle was detected”. in .NET