Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
                }
        }
    }
like image 464
Mike Flynn Avatar asked Oct 20 '18 15:10

Mike Flynn


1 Answers

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);
                    }
                }
            }
        }
like image 177
Mike Flynn Avatar answered Nov 16 '22 14:11

Mike Flynn