Last active
May 9, 2016 06:22
-
-
Save VisualBean/1aa7a614377c214e7d41 to your computer and use it in GitHub Desktop.
Full PagingFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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