Created
February 26, 2016 20:37
-
-
Save dataneek/c8b49dcf3eab2f6f157a to your computer and use it in GitHub Desktop.
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
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