我有一个通过OData V4控制器返回的Entity Framework对象。我返回了一个IQueryable,如果我在不加任何OData子句的情况下调用OData端点,我可以成功地执行此操作:
var content = response.Content.ReadAsAsync<IQueryable<Person>>();
下面是JSON格式的响应:
{
"@odata.context":"http://xxx:8082/odata/$metadata#Persons","value":[
{
"Id":"291b9f1c-2587-4a35-993e-00033a81f6d5",
"Active":true,
"Alert":"Some alerts for the Person",
"Comments":"Some comments for the Person"
}
]
}
当我开始使用OData,例如在复杂属性上使用$expand时,就会出现以下异常:无法将当前JSON对象(例如{"name":"value"})反序列化为类型“System.Linq.IQueryable '1 [xxx.Person]”,因为该类型需要一个JSON数组(例如[1,2, 3])才能正确反序列化。响应如下:
{
"@odata.context":"http://aqavnext01:8082/odata/$metadata#Persons","value":[
{
"Id":"291b9f1c-2587-4a35-993e-00033a81f6d5",
"Active":true,
"Alert":"Some alerts for the Person",
"Comments":"Some comments for the Person",
"Party":{
"Id":"291b9f1c-2587-4a35-993e-00033a81f6d5"
}
}
]
}
我正在使用Web Api返回的相同对象进行反序列化,所以我不明白为什么它会失败。当我应用$select时也有相同的问题。