sql - .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
解决方案
解决了。问题是模型正在使用[DataContract]
,这导致空对象。安全地删除它解决了这个问题。
推荐阅读
- c++ - QT 库 QObject::connect: 无法连接 (null)::finished() 到 MainWindow::updateGUI() 错误
- jenkins - 你如何使用jenkins ec2插件在aws上启动windows代理/从属
- azure - 仅限制 azure 应用程序以通过自定义域名获取访问权限
- python - 如何在 Python 中对 2 个不同的数据集使用分类器随机森林?
- php - 当我在输入中输入用户名名称 DOB 或电子邮件时,如何搜索相关行?
- php - PHP进程数组转换成var-key
- c - 将接收静态二维数组的函数转换为接收指针的函数
- c# - dotnet core program.cs 行为不同 - 无法创建“AppDbContext”类型的对象
- r - 在 R 中重命名和合并行
- analytics - 使用 Kinesis Analytics 分析事件和相关的缺失事件,及时分开?