根据你的描述,看来你需要添加[EnableQuery]
存储过程的控制器方法的属性。
以下实施对我有用:
In WebApiConfig.cs
:
builder.EntityType<Product>().Function("SomeFunction").ReturnsFromEntitySet<Product>("Products");
In ProductsController.cs
:
[HttpGet]
[EnableQuery]
public IHttpActionResult SomeFunction()
{
return Ok(products.FirstOrDefault(c => c.ID == 1));
}
在浏览器中:
GET http://localhost:54017/Products(1)/Default.SomeFunction()?$expand=Categories
gives
{
@odata.context: "http://localhost:54017/$metadata#Products",
value: [
{
ID: 1,
Name: "Some",
Categories: [
{
ID: 1,
Name: "Some"
}
]
}
]
}
更新于 2014 年 10 月 22 日:
我已经修改了您附加的代码并将其附在下面。如果有效的话你会尝试吗?
[HttpPost]
[EnableQuery(PageSize=10)]
public IHttpActionResult SomeFunction()
{
var results = db.SomeStoredProc().ToList();
return Ok(results);
}
类似的功能在我的测试中有效。这可行的原因是 Web API OData 处理$skip
, $top
,并自动为您寻呼。您无需担心将它们应用到您的结果中。来自客户端的查询选项将应用于您返回的整个集合。