使用ASP.NET Web API 2 OData服务我公开了以下端点:
/entity1('key')/entity2('key')/entity3('key')/entity4('key')
当我执行/entity1('key')/entity2('key')/entity3?$expand=entity4
时,我收到了正确 "@odata.context"
的JSON回复:
"@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": [
{
...
}
]
}
使用Microsoft的OData Client执行时会导致异常:
"The context URL 'https://host:port/$metadata#entity1('key')/entity2('key')/entity3('key')('key')/entity4' is invalid."
好吧不开玩笑。可能导致这种情况的原因是什么?
更新
我已经记录了问题here。我们会看到会发生什么。
答案 0 :(得分:0)
我找到了解决方法:
使用$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": [
{
...
}
]
}