【问题标题】:$select returning empty json result for Navigation Property$select 返回导航属性的空 json 结果
【发布时间】:2016-04-11 04:41:00
【问题描述】:

我在 web api 中使用 v4 的 oData。

我的 get all 调用正确地返回了整个集合(总共 3 个对象)。

http://localhost:9910/api/CommandsRest

但尝试仅选择 StoreCommand 会给我 3 个空白对象

http://localhost:9910/api/CommandsRest?$select=StoreCommand

尝试扩展 StoreCommand 给了我 StoreCommand 但没有给我它的导航属性

http://localhost:9910/api/CommandsRest?$expand=StoreCommand

我只想要主对象的 StoreCommand 属性及其所有导航属性。

【问题讨论】:

  • 请贴出Get方法的源代码。
  • Get 使用存储库中的 mongoDB 的 FindAll 方法返回 apicintroller 中实体的 IQueryable。还具有可查询属性。你想让我更新代码还是这个信息就足够了?
  • Queryable 属性已过时。您应该使用System.Web.OData.EnableQuery 属性。
  • 所以启用查询
  • 我的意思是它已经是 EnableQuery

标签: json asp.net-web-api odata asp.net-web-api-odata


【解决方案1】:

试试/CommandsRest?$select=StoreCommand&$expand=StoreCommand($expand=DictionaryVariables)。这将为您提供每个 StoreCommand 与 DictionaryVariables 属性展开内联。

如果您想要 所有 StoreCommand 的导航属性,则必须在嵌套的展开子句中明确列出它们。例如,$expand=DictionaryVariables,DictionaryNestedSplitVariables,DictionaryMultipleVariables,TabularVariables

请注意,我使用内存数据而不是 MongoDB 测试了这些 URI。

【讨论】:

  • 太棒了。所以我必须为整个图表做这件事?没有办法简单地得到完整的图表?
  • 不使用查询选项。如果您想要一种紧凑的方式来返回大图,请在执行相应 MongoDB 查询的 OData 函数上定义。详情请见Functions & Actions in Web API V2.2 for OData V4.0
  • 因为我使用 angular 的 $resource,所以我在服务器端有 ApiController,但我想只有使用 ODataController 时才能使用功能。 ?
  • 我以为你一直在使用 ODataController。我很惊讶EnableQuery 与 ApiController 一起工作。您可以使用“CommandsRest/StoreCommandSubgraph”之类的路由在 ApiController 上编写等效的 OData 函数。
猜你喜欢
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 2018-11-07
  • 2012-03-18
  • 1970-01-01
  • 2019-07-09
相关资源
最近更新 更多