Created
October 29, 2012 13:15
-
-
Save juristr/3973479 to your computer and use it in GitHub Desktop.
Programmatic GZip Action Filter for ASP.net MVC3
This file contains hidden or 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
public class GZipCompressionGlobalActionFilter : ActionFilterAttribute | |
{ | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
string acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"]; | |
if (string.IsNullOrEmpty(acceptEncoding)) return; | |
acceptEncoding = acceptEncoding.ToUpperInvariant(); | |
HttpResponseBase response = filterContext.HttpContext.Response; | |
if (acceptEncoding.Contains("GZIP")) | |
{ | |
response.AppendHeader("Content-encoding", "gzip"); | |
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); | |
} | |
else if (acceptEncoding.Contains("DEFLATE")) | |
{ | |
response.AppendHeader("Content-encoding", "deflate"); | |
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment