WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

Please make it easier to implement server-driven paging with custom queries

Open bitsforthought opened this issue 11 years ago • 5 comments

Currently server-driven paging is easy to support if you're using Web API OData with LINQ. ODataQueryOptions.ApplyTo() does the work for you. However, for services that don't use LINQ and instead process the OData query options ($filter, $orderby, etc.) and execute them directly, there is a lot of logic to implement. In particular, generating @odata.nextLink is non-trivial.

As a workaround, we are calling one of the overloads of ODataQueryOptions.GetNextLink() using reflection. It would be preferable if this method were public so that the logic can be re-used by anyone wanting to implement server-driven paging without using ApplyTo().

This is a small change, and if you agree with it I would be happy to make the change myself and submit a PR.

bitsforthought avatar Jun 03 '15 21:06 bitsforthought

Thanks @brjohnstmsft very much for the feedback. Please feel free to submit a PR.

congysu avatar Jun 06 '15 21:06 congysu

Created: https://github.com/OData/WebApi/pull/384

bitsforthought avatar Jun 08 '15 00:06 bitsforthought

Checkin 604b16c

VikingsFan avatar Jun 10 '15 03:06 VikingsFan

@brjohnstmsft I just ran into exactly the scenario you describe and created an issue here: https://github.com/OData/WebApi/issues/1633

Could you please advise on how to properly support NextLink and TotalCount using a purely custom query there? Even if I rely on the Request.GetNextPageLink(int) call, it still generates the link everytime so I wonder if I'd have to handle when to show and not to show it manually?

In my particular case, I cannot apply the query options since I'm using Dapper and stored procs to do the heavy lifting.

julealgon avatar Oct 03 '18 20:10 julealgon

@julealgon You will need to implement your own logic to determine when to set NextLink and TotalCount. Actually setting them is pretty easy:

request.ODataProperties().TotalCount = totalCount;
request.ODataProperties().NextLink = request.GetNextPageLink(pageSize);

where totalCount and pageSize are values that you have to calculate yourself. Tip: to detect whether results fit entirely within a page or are truncated (i.e. -- to help you decide whether you need a NextLink or not), request one more row/record/document from your underlying data store than the page size. If the extra row comes back, you need a NextLink, otherwise you don't.

bitsforthought avatar Oct 03 '18 22:10 bitsforthought