首页 > 解决方案 > .NET API 对象数组仅在以 JSON 形式返回时才变为空对象

问题描述

我有一个 .NET API,它使用模型正式呈现视图,但现在设置为将模型作为 JSON 直接返回给 API 调用者。使用模型渲染视图时,我有一个填充良好的对象数组。当返回严格的 JSON 数据时,它几乎丢失了所有这些数据。

运行调试器,返回时数据存在,但最终结果有一个空对象而不是对象数组。

伪代码:

obj.property1 = sqlQuery1.ToArray(); //Empty object if returned using Ok(result)
obj.property2 = sqlQuery2.ToArray(); //Empty object if returned using Ok(result)
obj.property3 = sqlQuery3.ToArray(); //This one comes through for some reason

//Pack these objects into array
return Ok(result); //Most properties become {}, debugging confirms they exist at this point
return View(result); //All properties render normally

标签: sql.netjsonapi

解决方案


解决了。问题是模型正在使用[DataContract],这导致空对象。安全地删除它解决了这个问题。


推荐阅读