Hello All,
Is anybody know, is it possible to compress my outgoing http data in IE? I perform ajax requests to a server and want to reduce amount of traffic in order to speed up my app.
Thanks, Egor
Hello All,
Is anybody know, is it possible to compress my outgoing http data in IE? I perform ajax requests to a server and want to reduce amount of traffic in order to speed up my app.
Thanks, Egor
The following is a common way to create a compression filter attribute:
public class CompressFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
HttpRequestBase request = actionContext.HttpContext.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (!string.IsNullOrWhiteSpace(acceptEncoding))
{
acceptEncoding = acceptEncoding.ToLowerInvariant();
HttpResponseBase response = actionContext.HttpContext.Response;
if (acceptEncoding.Contains("gzip"))
{
response.AddHeader("Content-encoding", "gzip");
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("deflate"))
{
response.AddHeader("Content-encoding", "deflate");
response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
}
}
}
}
Now you can decorate your controller with a [CompressFilter]
attribute. It will add a compression header to the response for browsers that support it, which IIS will pick up. If you have switched on Dynamic Compression, IIS will then output compressed responses.