1

I'm changing my MVC4 Api project to Web Api 2. I had the following filter to compress the response:

public class CompressResponseAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpRequestBase request = filterContext.HttpContext.Request;

        string acceptEncoding = request.Headers["Accept-Encoding"];

        if (!String.IsNullOrEmpty(acceptEncoding))
        {
            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);
            }
        }
    }
}

However, the above code doesn't work with Web Api 2. Are there any working examples to handle compression in code (not IIS)?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • http://stackoverflow.com/questions/10443588/mvc4-webapi-compress-get-method-response – Tewr Mar 23 '14 at 15:03

0 Answers0