VisualBean
12/14/2015 - 11:42 AM

Full PagingFilter.cs

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;
    }  
}