Search code examples
asp.net-mvcexceptionaction-filter

CompressFilter conflicting with ExceptionHandlerFilter in asp.net MVC


I am not getting my ExceptionHandling hit when I have the CompressFilter on the action and their is an error. No response is returned on the request. If I remove the Compress filter then it returns the error array just fine. How can I skip the compress filter on an error, or have it hit second?

Controller Action

 [HttpPost, CompressAttribute]
 public virtual ActionResult Builder()

Global.asax

GlobalConfiguration.Configuration.Filters.Add(new ExceptionHandlingAttribute());

CompressFilter

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class CompressAttribue : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
                var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
                if (string.IsNullOrEmpty(encodingsAccepted)) return;

                encodingsAccepted = encodingsAccepted.ToLowerInvariant();
                var response = filterContext.HttpContext.Response;

                if (encodingsAccepted.Contains("gzip"))
                {
                    response.AppendHeader("Content-encoding", "gzip");
                    response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                }
                else if (encodingsAccepted.Contains("deflate"))
                {
                    response.AppendHeader("Content-encoding", "deflate");
                    response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                }
        }
    }

Solution

  • I moved it to the OnActionExecuted and it worked since it contains an Exception property.

    public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                base.OnActionExecuted(filterContext);
    
                if (filterContext.Exception == null)
                {
                    var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
                    if (!encodingsAccepted.IsBlank())
                    {
                        encodingsAccepted = encodingsAccepted.ToLowerInvariant();
                        var response = filterContext.HttpContext.Response;
    
                        if (encodingsAccepted.Contains("gzip"))
                        {
                            response.AppendHeader("Content-encoding", "gzip");
                            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                        }
                        else if (encodingsAccepted.Contains("deflate"))
                        {
                            response.AppendHeader("Content-encoding", "deflate");
                            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                        }
                    }
                }
            }