Created
June 14, 2013 09:07
-
-
Save tegaralaga/5780531 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.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