OData pagination with WebApi ( $inlinecount )

13,122

Solution 1

Try this approach: http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-package/

It uses the latest Web API OData package.

Until the finalized Web API OData package is shipped (sometime this fall, should be around in November), when the $inlinecount is supported out of the box, this solution is likely the best bet.

Solution 2

You can use $inlinecount=allpages in the query to get the count of all the entities in the results without the top and skip. For example:

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

Returns a single product but also inline count of 9 (since there are 9 products in the entity set).

Solution 3

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.

Share:
13,122
amateur
Author by

amateur

Updated on June 04, 2022

Comments

  • amateur
    amateur about 2 years

    I am using OData to paginate a long list of items returned from a web api call. I can filter the data via the url with the start and end index.

    The question I have is, how do I know the total number of items? So I can display Page 1 of 3 (20 items) on my mobile device which calls the web api.

  • amateur
    amateur about 12 years
    Thanks but found the following link since which states it is not supported out of the box -> thedevstop.wordpress.com/2012/04/05/…
  • Josh E
    Josh E over 11 years
    attempting this with Microsoft.AspNet.WebApi.OData version 0.1.0-alpha-120815 returns a 400 - {"Message":"The query parameter '$inlinecount' is not supported."}
  • Dejan
    Dejan almost 9 years
    $inlinecount is deprecated. See my answer here: stackoverflow.com/a/31004759/331281
  • Yasin Kilicdere
    Yasin Kilicdere over 8 years
    you forgot the '$' sign on parameter 'skip'