10

我正在使用 OData 对从 Web api 调用返回的一长串项目进行分页。我可以通过带有开始和结束索引的 url 过滤数据。

我的问题是,我怎么知道项目的总数?所以我可以在调用 web api 的移动设备上显示第 1 页,共 3 页(20 项)。

4

3 回答 3

3

您可以$inlinecount=allpages在查询中使用以获取结果中所有实体的计数,而无需顶部和跳过。例如:

http://services.odata.org/OData/OData.svc/Products?$top=1&skip=1&$inlinecount=allpages

返回单个产品,但内联计数为 9(因为实体集中有 9 个产品)。

于 2012-05-22T17:10:33.470 回答
3

试试这种方法: http: //www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-package/

它使用最新的 Web API OData 包。

在最终确定的 Web API OData 包发布之前(今年秋天的某个时候,应该在 11 月左右),当 $inlinecount 支持开箱即用时,这个解决方案可能是最好的选择。

于 2012-10-02T13:29:52.587 回答
2

I had the exact issue last week. Check out Extending your ASP.NET Web API responses with useful metadata

I used this post and sample code to get a paging grid up and running using OData. As detailed in the sample I created a delegating handler to capture the HttpResponseMessage and wrap it in custom metadata that includes an item count. A custom attribute, CustomQueryableAttribute, is also created that inherits the default QueryableAttribute.

It may sound a little complex here but is actually pretty simple to implement. I had something up and running in about 30 minutes.

Hopefully future versions of the Web API have more complete OData support.

EDIT: Odata support will NOT be shipping with the Web API. The queryable attribute is being removed for the RTM release. More complete OData support will be available sometime after the initial relase via a separate Nuget package.

于 2012-10-02T17:11:15.283 回答