If your action method returns IQueryable<TItem> then Web API itself will apply $filter, $orderby, $skip and $top OData system query options that are present in the URI's query string. This is handy because the expression language of $filter can get quite
complex, even for fairly limited subsets of the full language.
If, like me, your data source doesn't expose IQueryable<T>, then you'll have to take $skip and $top args in your action methods more directly - like this:-
You need the FromUri attribute to map "$skip" to a param name "skip" because C# doesn't allow params and variables starting with $.
If $skip isn't present, then skip will be null - interpret this as zero.
If $top isn't present, then top will be null - interpret this as "all items". But what I do is impose a maximum limit. I.e. if top is null or exceeds N, then set to N.
An action filter seems to be what you are looking for. Inherit the ActionFilterAttribute and in the ActionExecutedMethod you can limit the number of items your IEnumerable or IQueryable is returning. Be careful with ordering of those trunactes though. you
might break the $orderby's.
A quick sample that I came up with to get you started :)
public class LimitAttribute<T> : ActionFilterAttribute
{
private int _pageSize;
public LimitAttribute(int pageSize = 10)
{
_pageSize = pageSize;
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
IEnumerable<T> responseCollection;
if (actionExecutedContext.Result.TryGetObjectValue(out responseCollection))
{
actionExecutedContext.Result.TrySetObjectValue(responseCollection.Take(_pageSize));
}
}
}
bvitale
0 Points
3 Posts
Mandating paging
Mar 08, 2012 05:49 PM|LINK
For certain GETs that return lists, I never want to return more than a fixed number of items.
$top and $skip are great for this -- if the client sends them.
How can I determine if those are part of the query string?
For purposes of returning an HTTP error code, or at least doing some default paging on their behalf.
awebb
Member
204 Points
91 Posts
Re: Mandating paging
Mar 09, 2012 06:08 AM|LINK
If your action method returns IQueryable<TItem> then Web API itself will apply $filter, $orderby, $skip and $top OData system query options that are present in the URI's query string. This is handy because the expression language of $filter can get quite complex, even for fairly limited subsets of the full language.
If, like me, your data source doesn't expose IQueryable<T>, then you'll have to take $skip and $top args in your action methods more directly - like this:-
You need the FromUri attribute to map "$skip" to a param name "skip" because C# doesn't allow params and variables starting with $.
If $skip isn't present, then skip will be null - interpret this as zero.
If $top isn't present, then top will be null - interpret this as "all items". But what I do is impose a maximum limit. I.e. if top is null or exceeds N, then set to N.
raghuramn
Member
248 Points
64 Posts
Microsoft
Re: Mandating paging
Mar 10, 2012 01:29 AM|LINK
An action filter seems to be what you are looking for. Inherit the ActionFilterAttribute and in the ActionExecutedMethod you can limit the number of items your IEnumerable or IQueryable is returning. Be careful with ordering of those trunactes though. you might break the $orderby's.
A quick sample that I came up with to get you started :)
public class LimitAttribute<T> : ActionFilterAttribute { private int _pageSize; public LimitAttribute(int pageSize = 10) { _pageSize = pageSize; } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { IEnumerable<T> responseCollection; if (actionExecutedContext.Result.TryGetObjectValue(out responseCollection)) { actionExecutedContext.Result.TrySetObjectValue(responseCollection.Take(_pageSize)); } } }