Skip to content

Instantly share code, notes, and snippets.

@VisualBean
Last active May 9, 2016 06:22
Show Gist options
  • Save VisualBean/1aa7a614377c214e7d41 to your computer and use it in GitHub Desktop.
Save VisualBean/1aa7a614377c214e7d41 to your computer and use it in GitHub Desktop.
Full PagingFilter.cs
public class PagingFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext context)
{
int offset = 0;
int limit = 0;
var queryString = HttpUtility.ParseQueryString(context.Request.RequestUri.Query);
if(queryString.Count == 0) return;
int.TryParse(queryString.Get("offset"), out offset);
int.TryParse(queryString.Get("limit"), out limit);
//Get our returned entityModel from the response
IEnumerable model = null;
context.Response.TryGetContentValue(out model);
if (model == null) return;
//Change response if limit or offset is not zero
IEnumerable result = null;
if ((limit + offset) > 0)
{
result = model.Take(limit).Skip(offset);
}
var objectContent = context.ActionContext?.Response?.Content as ObjectContent;
if (objectContent != null)
objectContent.Value = result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment