首页 新闻 会员 周边 捐助

MVC 设置全局GZIP压缩后,遇到异常黄屏出现乱码,怎么办?

0
悬赏园豆:15 [已关闭问题] 关闭于 2013-03-26 21:31

有什么办法避免呢?

跟这个问题差不多,http://q.cnblogs.com/q/44558/

看了,也运用了,还是没用。

我照着那上面的方法写,也没用,一如既往的出现乱码。我不知道GZIP流怎么解压缩啊?

使用new GZIPStream(response.Filter,CompressionMode.Decompress) 也不行。

 

    public class CompressFilterAttribute : ActionFilterAttribute, IExceptionFilter
    {
        const CompressionMode compress = CompressionMode.Compress;
        const CompressionMode decompress = CompressionMode.Decompress;


        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            object[] actionFilter = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoCompress), false);
            object[] controllerFilter = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(NoCompress), false);
            if (controllerFilter.Length == 1 || actionFilter.Length == 1)
            {
                return;
            }

            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                return;
            }


            HttpRequestBase request = filterContext.HttpContext.Request;
            HttpResponseBase response = filterContext.HttpContext.Response;
            string acceptEncoding = request.Headers["Accept-Encoding"];

            if (acceptEncoding == null)
                return;

            if (acceptEncoding.ToLower().Contains("gzip"))
            {
                response.Filter = new GZipStream(response.Filter, compress);
                response.AppendHeader("Content-Encoding", "gzip");


            }
            else if (acceptEncoding.ToLower().Contains("deflate"))
            {
                response.Filter = new DeflateStream(response.Filter, compress);
                response.AppendHeader("Content-Encoding", "deflate");
            }
        }
        #endregion


        public void OnException(ExceptionContext filterContext)
        {
            var response = filterContext.HttpContext.Response;
            response.Filter = new GZipStream(response.Filter, decompress);
        }
    }

 

虔城墨客的主页 虔城墨客 | 初学一级 | 园豆:185
提问于:2013-03-20 15:22
< >
分享
所有回答(1)
0

gzip在IIS设置就好了。为什么要写这段代码呢

调调儿 | 园豆:155 (初学一级) | 2013-03-21 18:30

有需求啊,有办法吗?      

支持(0) 反对(0) 虔城墨客 | 园豆:185 (初学一级) | 2013-03-22 11:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册