title: ASP.NET MVC - Response.Filter is null when using ActionFilterAttribute in RegisterGlobalFilters()
body: <p>I want to use G-ZIP on my website, I googled the following code:</p>
  
  <pre><code>public class CompressAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
          if (!string.IsNullOrEmpty(acceptEncoding))
          {
              acceptEncoding = acceptEncoding.ToLower();
              var 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);
              }
          }
      }
  }
  </code></pre>
  
  <p>It works fine when I set the attribute to a Controller or an Action.</p>
  
  <pre><code>[Compress]
  public class PostController : Controller
  </code></pre>
  
  <p>I don't want to manully do this on every piece of code, so I registered this attribute in </p>
  
  <pre><code>public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  {
      filters.Add(new HandleErrorAttribute());
      filters.Add(new CompressAttribute());
  }
  </code></pre>
  
  <p>But when I run the application, exception came on this line of code:</p>
  
  <pre><code>response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
  </code></pre>
  
  <p>the response.Filter is null.</p>
  
  <p>I want to know why this is happening and how to solve this. Thanks!</p>
  
  <p><strong>- Update:</strong></p>
  
  <p><strong>I found that the exception happens only when the controller contains a child action, and it's being invoked.</strong></p>