MVC 3 COM pression过滤器造成混乱的输出 [英] MVC 3 compression filter causing garbled output

查看:242
本文介绍了MVC 3 COM pression过滤器造成混乱的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有名为com的pressAttribute一个自定义属性,它被设置为在Global.asax中一个全球性的过滤器。它使用反射来检查当前操作方法的返回类型,如果是的ViewResult,它融为一体presses无论是使用gzip或deflate输出。它的工作原理除非一个页面抛出一​​个500服务器错误就好了。如果遇到的,而不是显示.NET错误页的错误,我得到了一堆这样的:

`我%安培; / M {JJT

显然它试图连接code这是导致问题的500服务器错误页面。什么是处理这个问题的最佳方式?

下面的过滤器code:

 公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
        {
            MethodInfo的actionMethodInfo = Common.GetActionMethodInfo(filterContext);
            如果(GetReturnType(actionMethodInfo).ToLower()=的ViewResult!)回报;            HTT prequestBase请求= filterContext.HttpContext.Request;            字符串acceptEncoding = request.Headers [的Accept-Encoding];            如果(string.IsNullOrEmpty(acceptEncoding))回报;            acceptEncoding = acceptEncoding.ToUpperInvariant();            HTT presponseBase响应= filterContext.HttpContext.Response;            如果(acceptEncoding.Contains(GZIP))
            {
                response.AppendHeader(内容编码,gzip的);
                response.Filter =新WebCom pressionStream(response.Filter,COM pressionType.GZip);
            }
            否则如果(acceptEncoding.Contains(DEFLATE))
            {
                (内容编码,放气)response.AppendHeader;
                response.Filter =新WebCom pressionStream(response.Filter,COM pressionType.Deflate);
            }
        }


解决方案

好了,所以我能够在Application_Error事件清除Response.Filter属性来解决此问题:

 公共无效的Application_Error(对象发件人,EventArgs的发送)
{
    Response.Filter.Dispose();
}

知道是否有这样做虽然比较正确的做法...

So, I have a custom attribute called CompressAttribute which is set up as a global filter in global.asax. It uses reflection to examine the return type of the current action method and if it is "ViewResult" it compresses the output using either GZip or Deflate. It works just fine except if a page throws a 500 Server Error. If an error is encountered, instead of displaying the .NET error page, I get a bunch of this:

��������`I�%&/m�{J�J��t��

Apparently it's attempting to encode the 500 Server Error page which is causing problems. What's the best way to handle this?

Here's the filter code:

public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            MethodInfo actionMethodInfo = Common.GetActionMethodInfo(filterContext);
            if (GetReturnType(actionMethodInfo).ToLower() != "viewresult") return;

            HttpRequestBase request = filterContext.HttpContext.Request;

            string acceptEncoding = request.Headers["Accept-Encoding"];

            if (string.IsNullOrEmpty(acceptEncoding)) return;

            acceptEncoding = acceptEncoding.ToUpperInvariant();

            HttpResponseBase response = filterContext.HttpContext.Response;

            if (acceptEncoding.Contains("GZIP"))
            {
                response.AppendHeader("Content-encoding", "gzip");
                response.Filter = new WebCompressionStream(response.Filter, CompressionType.GZip);
            }
            else if (acceptEncoding.Contains("DEFLATE"))
            {
                response.AppendHeader("Content-encoding", "deflate");
                response.Filter = new WebCompressionStream(response.Filter, CompressionType.Deflate);
            }
        }

解决方案

Ok so I was able to resolve this by clearing the Response.Filter property in the Application_Error event:

public void Application_Error(object sender, EventArgs e)
{
    Response.Filter.Dispose();
}

Wondering if there's a more correct way to do it though...

这篇关于MVC 3 COM pression过滤器造成混乱的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆