CompressFilter conflicting with ExceptionHandlerFilter in asp.net MVC
Asked Answered
U

1

6

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);
                }
        }
    }
Unmentionable answered 20/10, 2018 at 15:38 Comment(3)
when is the compress added in relation to the error filterMccune
Updated and added to the questionUnmentionable
Have you checked this? #9283102Foetid
U
3

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);
                    }
                }
            }
        }
Unmentionable answered 24/10, 2018 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.