Skip to content

Instantly share code, notes, and snippets.

@tegaralaga
Created June 14, 2013 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tegaralaga/5780531 to your computer and use it in GitHub Desktop.
Save tegaralaga/5780531 to your computer and use it in GitHub Desktop.
using System;
using System.IO.Compression;
using System.Web.Mvc;
namespace WellThen
{
public class Compress : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
if (string.IsNullOrEmpty(encodingsAccepted)) return;
encodingsAccepted = encodingsAccepted.ToLowerInvariant();
var response = filterContext.HttpContext.Response;
if (encodingsAccepted.Contains("deflate"))
{
response.AppendHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
else if (encodingsAccepted.Contains("gzip"))
{
response.AppendHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment