Search code examples
c#odataasp.net-web-apiasp.net-web-api2

$inlinecount not working on ApiController using ApplyTo


I'm trying to use the OData filtering and paging capabilities in a standard Web API 2.2 ApiController. For this, I have to rewrite the request URL to conform to the OData v4 standards. My controller looks like this:

public GridPage Get([FromUri] GridSearchCriteria criteria)
{
    Request.RequestUri = ... // convert querystring to OData v4
    var context = new ODataQueryContext(MyEdmModel.Instance, typeof(Delivery), null);
    ODataQueryOptions<Delivery> options = new ODataQueryOptions<Delivery>(context, Request);

    IQueryable<Delivery> deliveries = ... // use EF to load deliveries from DB
    var result = (IQueryable<Delivery>)options.ApplyTo(deliveries); // BTW, I wonder why there is no generic overload of ApplyTo?

    // fill and return a GridPage
    ...
}

So far everything works nice as expected.

Now, I'm interested in the total count of the filtered items and thus I've added $inlinecount=allpages to the query string. The resulting request URI looks like this:

http://localhost:54026/.../deliveries/page?$top=10&$skip=0&$inlinecount=allpages}

Then, I'm trying to retrieve the total count like this (after the call to ApplyTo):

long? totalCount = Request.ODataProperties().TotalCount;

Unfortunately, totalCount always stays null no matter what I try. I've also tried using:

object totalCount;
Request.Properties.TryGetValue("System.Web.OData.TotalCount", out totalCount);

but no luck. A peek into the Request properties reveals that there is an entry under System.Web.OData.Properties but all its properties are uninitialized (Model is null, NextLink is null, TotalCount is null, etc.).

Does anybody have an idea why this isn't working? BTW, I'm using Microsoft.AspNet.OData v5.6.


Solution

  • I had to specify $count=true instead of $inlinecount=allpages. According to https://damienbod.wordpress.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/ this was a breaking change somewhere.