为什么 Web Api 返回带有“$ref="的空 json 结果? [英] Why is the Web Api returning empty json results with "$ref="?
问题描述
在 Web Api 中返回任何结果之前,我在 EF5 中禁用了延迟加载和代理创建.一切正常.但是,当我开始 Include()
其他实体以进行急切获取时,一些 JSON 对象如下所示:
I disabled lazy loading and proxy creation in EF5 before returning any results in the Web Api. Everything worked fine. However, when i start to Include()
other entities for eager fetching, some of the JSON objects look like this:
{
$ref: "14"
},
.. correct objects ..
{
$ref: "6"
},
..
包含在结果列表中的这个 "$ref"
是什么?
What is this "$ref"
that gets included in the list of results?
推荐答案
它来自 JSON.NET 序列化程序.它保留了对象引用.查看以下博客文章
.我猜您的对象图中有一些循环引用,并且 JSON.NET 正在尽最大努力在不崩溃的情况下序列化这个 EF 结构(这是大多数 JSON 序列化程序无论如何都会做的).但这不是标准.
It's coming from the JSON.NET serializer. It's preserving object references. Take a look at the following blog post
. I guess you have some circular references in your object graph and JSON.NET is doing it's best in a desperate effort to serialize this EF structure without crashing (which is what most JSON serializers would do anyway). It's not a standard though.
个人建议:永远不要序列化您的 EF 实体.始终定义视图模型并让您的 Web API 方法返回仅包含您感兴趣的属性的视图模型.
Personal advice: never serialize your EF entities. Always define view models and have your Web API methods return view models containing only the properties you are interested in.
这篇关于为什么 Web Api 返回带有“$ref="的空 json 结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!