从API返回结果列表时,异常错误[英] Unusual error when returning list of results from API

本文是小编为大家收集整理的关于从API返回结果列表时,异常错误的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在从.NET Core 3.x API返回数据,但请继续遇到这个奇怪的错误:

system.text.json.jsonexception:检测到不支持的对象周期.这可能是由于一个周期引起的,或者如果对象深度大于最大允许的深度32.

.

这是我在使用数据后立即将数据返回的内容.

     var bookings = bookingData
        .Select(x => new SpecialTaskVm(new TaskViewModel(x, null))
        {
            client = x.Client,
            carer = x.Carer,
            carer2 = x.Carer2
        })
        .ToList();

我尝试更改控制器方法类型

我希望数据正常返回,而不是500错误

推荐答案

您的Client和Carer都通过ClientCarer.

互相间接引用

而不是服务Client和Carer up,我建议创建一个新的类/匿名类型,仅具有所需的特定属性.

本文地址:https://www.itbaoku.cn/post/1556769.html

问题描述

I'm returning data from a .NET Core 3.X API but keep getting this strange error:

System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.

Here is what I'm doing with the data before immediately returning it after.

     var bookings = bookingData
        .Select(x => new SpecialTaskVm(new TaskViewModel(x, null))
        {
            client = x.Client,
            carer = x.Carer,
            carer2 = x.Carer2
        })
        .ToList();

I have tried changing the controller method type

I expect the data to return normally rather than a 500 error

推荐答案

Your Client and Carer both have indirect references to each other via ClientCarer.

Rather than serving Client and Carer up I'd suggest creating a new class / anonymous type with just the specific properties you need.