代码之家  ›  专栏  ›  技术社区  ›  Mike Flynn

压缩筛选器与asp.net MVC中的ExceptionHandlerFilter冲突

  •  6
  • Mike Flynn  · 技术社区  · 7 年前

    我没有得到我的异常处理命中时,我有压缩过滤器的行动,他们是一个错误。请求未返回响应。如果我删除压缩过滤器,那么它返回的错误数组就可以了。我怎样才能跳过一个错误的压缩过滤器,或者让它第二次命中?

     [HttpPost, CompressAttribute]
     public virtual ActionResult Builder()
    

    全球.asax

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

    压缩过滤器

    [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);
                    }
            }
        }
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Mike Flynn    7 年前

    我把它移到 OnActionExecuted 因为它包含了一个异常属性。

    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);
                        }
                    }
                }
            }