Skip to content

Instantly share code, notes, and snippets.

@dataneek
Created February 26, 2016 20:37
Show Gist options
  • Save dataneek/c8b49dcf3eab2f6f157a to your computer and use it in GitHub Desktop.
Save dataneek/c8b49dcf3eab2f6f157a to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Net.Http;
using System.Web.Http.Filters;
using Newtonsoft.Json;
using PaginableCollections;
public class PaginableResultActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response == null || !actionExecutedContext.Response.IsSuccessStatusCode)
return;
IPaginable paginable;
if (actionExecutedContext.Response.TryGetContentValue<IPaginable>(out paginable))
{
actionExecutedContext.Response.Headers.Add(
"X-Paginable",
JsonConvert.SerializeObject(
new PaginationHeader(paginable),
actionExecutedContext.ActionContext.ControllerContext.Configuration.Formatters.JsonFormatter.SerializerSettings));
}
}
public class PaginationHeader
{
public PaginationHeader(IPaginable paginable)
{
this.PageNumber = paginable.PageNumber;
this.ItemCountPerPage = paginable.ItemCountPerPage;
this.TotalItemCount = paginable.TotalItemCount;
this.TotalPageCount = paginable.TotalPageCount;
}
public int ItemCountPerPage { get; set; }
public int PageNumber { get; set; }
public int TotalItemCount { get; set; }
public int TotalPageCount { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment