Vipul
Vipul

Reputation: 1583

After enabling queryable support in ASP.NET Web API $count is not working

I am working on a Web API application where we have enabled the queryable support in ASP.NET Web API.

Issue: $skip, $top etc are working but $count & $inlineCount is not working.

My API controller is not derived from ODataController, looks like below

public class UsersSvcController : ApiController

[Queryable]
public IQueryable<VMUser> Get()
{
        return userBL.GetAll();
}
}

I came to know that Odata is not supported with Generic type as of now and so $count is also not working.

Is there any way where I can extend QueryableAttribute class and write my own code to get the total records?

Upvotes: 1

Views: 870

Answers (1)

Feng Zhao
Feng Zhao

Reputation: 2995

Since you are not using ODataController, the "count" won't be serialized into response payload.

The "count" serialization is done in the ODataFeedSerializer.

If you want the "count" in the response payload, you need to use ODataController.

If you just need the "count" value, you can get it from the response as blow.

1.For V3:

response.RequestMessage.Properties.TryGetValue("MS_InlineCount", out countValue)

Or use extension method under namespace "System.Web.Http.OData.Extensions"

response.RequestMessage.ODataProperties().TotalCount

2.For V4:

response.RequestMessage.Properties.TryGetValue("System.Web.OData.TotalCount", out countValue)

Or use extension method under namespace "System.Web.OData.Extensions"

response.RequestMessage.ODataProperties().TotalCount

Upvotes: 2

Related Questions