[英]A possible object cycle was detected when serializing entity-model to JSON
I have a webapi project that written using C#
and built on the top of ASP.NET Core/5 framework.我有一个使用
C#
编写并构建在 ASP.NET Core/5 框架之上的 webapi 项目。
The API return JSON data. API 返回 JSON 数据。
Here is an example of one endpoint这是一个端点的示例
[HttpGet]
public async Task<ActionResult<TModel[]>> QueryAsync(
[FromQuery(Name = "filter")] string filter = null,
[FromQuery(Name = "expand")] string expand = null)
{
IQueryable<TModel> query = GetQuery(filter, expand);
var models = await query.ToArrayAsync();
return Ok(models);
}
In the above request, the expand
will tell the server which navigation properties to expand.在上面的请求中,
expand
会告诉服务器要扩展哪些导航属性。 When the user request to expand a property, I get the following error当用户请求扩展属性时,我收到以下错误
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
Following the instruction from the error, I added the following to the Startup class按照错误中的说明,我在 Startup 类中添加了以下内容
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
// added this
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
The above change did fix the problem!上述更改确实解决了问题! However, it introduced another problem.
然而,它引入了另一个问题。 The new problem is that it changed the structure of the output.
新的问题是它改变了输出的结构。
The output went from something like this输出来自这样的东西
{
// properties
"relations": [
{
// relation 1
"parent": null
},
{
// relation 2
"parent": null
}
]
}
to something like this像这样的事情
{
"$id": "1",
// properties
"relations": {
"$id": "2",
"$values": [
{
// relation 1 properties
"$id": "3",
"parent": {
"$ref": 1
}
},
{
// relation 2 properties
"$id": "3",
"parent": {
"$ref": 1
}
}
]
}
}
Is there a way to not change the structure of the output and instead ignore the circular reference?有没有办法不改变输出的结构而是忽略循环引用?
There is a similar question here, where one of the answers mentions the same issue you're having: .Net Core 3.0 possible object cycle was detected which is not supported这里有一个类似的问题,其中一个答案提到了您遇到的相同问题: .Net Core 3.0 possible object cycle was detected that is not supported
Some of the things mentioned there:那里提到的一些事情:
If you're on .NET 6 you can use如果您使用的是 .NET 6,则可以使用
JsonSerializerOptions options = new()
{
ReferenceHandler = ReferenceHandler.IgnoreCycles,
WriteIndented = true
};
alternatively, depending on what you need to achieve, you might want to just ignore looping properties altogether.或者,根据您需要实现的目标,您可能只想完全忽略循环属性。
The proper solution though would be to find out where exactly your references are looping and exclude the property(s) in question from serialization, or supply them in a format that doesn't loop (such as an id or similar)正确的解决方案是找出引用的确切位置循环并从序列化中排除有问题的属性,或者以不循环的格式(例如 id 或类似格式)提供它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.