I added Blazor components in my ASP.NET Core MVC application, when I am passing a model to the component, I got this error:
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 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.
So, I tried add all these options in my startup.cs
:
services.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(options => {
options.JsonSerializerOptions.MaxDepth = 256;
});
services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(opt =>
{
opt.SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
opt.SerializerOptions.MaxDepth = 256;
});
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
services.AddMvc(opt => { ... })
.AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
});
But none of them work, nothing happens, and I always get the same error.
I use the component like this:
<component type="typeof(CompBlazor)" render-mode="ServerPrerendered" param-Client="Model"/>
This is the code in my Blazor component:
@code {
[Parameter]
public Client cli {get;set;}
}
No matter what I do, I always get that same error.