MVC启用全局GZIP压缩后

MVC启用全局GZIP压缩后

本文介绍了MVC启用全局GZIP压缩后,异常页面出现乱码,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个压缩过滤器:CompressAttribute,设置了一个全局过滤器,但是出现了问题,每当抛出异常时,也会被压缩,所以页面会出现乱码。

这个是常规文件压缩功能代码

  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())
{
返回;
}


HttpRequestBase request = filterContext.HttpContext.Request;
HttpResponseBase response = filterContext.HttpContext.Response; string acceptEncoding = request.Headers [ Accept-Encoding ];
if (acceptEncoding == null
返回;

if (acceptEncoding.ToLower()。包含( 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);
}
}





我想,继承IException接口实现,每当出现异常时,我都会GZIP压缩解压缩,但没有效果,或者仍然出现凌乱的代码问题。以下是我的解压缩代码。



  public   void  OnException(ExceptionContext filterContext)
{
string acceptEncoding = filterContext.HttpContext.Request.Headers [ Accept-Encoding];
if String .IsNullOrEmpty(acceptEncoding))返回;
var response = filterContext.HttpContext.Response;
acceptEncoding = acceptEncoding.ToUpperInvariant();
var obj = new GZipStream(response.Filter,decompress);
if (acceptEncoding.Contains( GZIP ))
{
// response.AppendHeader(Content-Encoding ,gzip);
response.Filter = obj;
}
else if (acceptEncoding.Contains( DEFLATE))
{
// response.AppendHeader(Content-Encoding,deflate);
response.Filter = new DeflateStream(response.Filter,decompress);
}
}





我不知道我哪里错了,请大家帮帮我,谢谢!

解决方案


I wrote a compressed filter: CompressAttribute, set up a global filter, but there was a problem, whenever an exception is thrown, also will be compressed, so the page will appear garbled.
This is a regular file compression feature code

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



I thought, inheritance IException interface implementations, whenever abnormal, I on GZIP compression decompression, but no effect, or still appear messy code problem. The following is my decompression code.

public void OnException(ExceptionContext filterContext)
    {
        string acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
        if (String.IsNullOrEmpty(acceptEncoding)) return;
        var response = filterContext.HttpContext.Response;
        acceptEncoding = acceptEncoding.ToUpperInvariant();
        var obj = new GZipStream(response.Filter, decompress);
        if (acceptEncoding.Contains("GZIP"))
        {
         //   response.AppendHeader("Content-Encoding", "gzip");
            response.Filter = obj;
        }
        else if (acceptEncoding.Contains("DEFLATE"))
        {
            //   response.AppendHeader("Content-Encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, decompress);
        }
    }



I don''t know where I was wrong, ask everybody to help me,thank you!

解决方案


这篇关于MVC启用全局GZIP压缩后,异常页面出现乱码,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 17:13