-
-
Save LSTANCZYK/0babed4b93d612c862d7cb8fe9af991e to your computer and use it in GitHub Desktop.
Programmatic GZip Action Filter for ASP.net MVC3
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
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