加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 与asp.net MVC中的ExceptionHandlerFilter冲突的

发布时间:2020-12-16 03:52:33 所属栏目:asp.Net 来源:网络整理
导读:当我在动作上有CompressFilter并且它们是错误时,我没有得到我的ExceptionHandling命中.请求未返回任何响应.如果我删除压缩过滤器然后它返回错误数组就好了.如何在错误上跳过压缩过滤器,或者让它达到第二个? 控制器动作 [HttpPost,CompressAttribute] public
当我在动作上有CompressFilter并且它们是错误时,我没有得到我的ExceptionHandling命中.请求未返回任何响应.如果我删除压缩过滤器然后它返回错误数组就好了.如何在错误上跳过压缩过滤器,或者让它达到第二个?

控制器动作

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

解决方法

我将它移动到OnActionExecuted并且它有效,因为它包含Exception属性.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读