[c#]OData $expand 包含不正确的上下文链接

标签: Asp.net .Net OData C#
发布时间: 2017/2/25 3:48:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

使用 ASP.NET Web API 2 OData 服务我暴露了下列终结点︰

/entity1('key')/entity2('key')/entity3('key')/entity4('key')

当我执行 /entity1('key')/entity2('key')/entity3?$expand=entity4 我收到一个 JSON 响应与正确 "@odata.context" :

"@odata.context": "https://host:port/$metadata#entity1('key')/entity2('ket')/entity3",
 "value": [
{
  ...,
  "entity4@odata.context": "https://host:port/$metadata#entity1('key')/entity2('key')/entity3('key')/entity4",
  "entity4": [
    {
      ...
    }
  ]
},
...

当我执行 /entity1('key')/entity2('key')/entity3('key')?$expand=entity4 然而,我接收不正确 "@odata.context" 有两个密钥︰

"@odata.context": "https://host:port/$metadata#entity1('key')/entity2('ket')/entity3",
 "value": [
{
  ...,
  "entity4@odata.context": "https://host:port/$metadata#entity1('key')/entity2('key')/entity3('key')('key')/entity4",
  "entity4": [
    {
      ...
    }
  ]
}

执行与微软的 OData 客户端时,这会导致异常︰

"The context URL 'https://host:port/$metadata#entity1('key')/entity2('key')/entity3('key')('key')/entity4' is invalid."

也没有在开玩笑。可能什么原因这?

更新

我已经登录的问题在这里。我们会看到发生了什么。

解决方法 1:

我找到一种解决方法︰

使用 $filter 而不是密钥︰ /entity1('key')/entity2('key')/entity3?$filter=Key eq 'key'&$expand=entity4 提供的正确的响应︰

"@odata.context": "https://host:port/$metadata#entity1('key')/entity2('ket')/entity3",
"value": [
{
  ...,
  "entity4@odata.context": "https://host:port/$metadata#entity1('key')/entity2('key')/entity3('key')/entity4"    ,
  "entity4": [
    {
      ...
    }
  ]
}
赞助商