在ASP.NET / IIS7中使用Gzip时出现乱码错误页面输出

16

我已经在我的网站上实现了Rick Strahl的GZipEncodePage方法,对于网站本身来说它非常有效。但是,当我的代码抛出异常时,"服务器错误"页面看起来像这样:

乱码
(来源:x01.co.uk)

我尝试通过挂钩Application_Error来尝试删除GZip头文件,但是没有成功。我应该如何在发生错误时取消GZip压缩?

2个回答

21

我知道这个问题已经非常过时了。

在Application_Error中,从Response中删除Filters,像这样:

 protected void Application_Error(Object sender, EventArgs e)
 {
    HttpApplication app = sender as HttpApplication;
    app.Response.Filter = null;
 }

希望这能对任何人有所帮助。


1
对于任何想知道在哪里放置这段代码的人...将其倾倒到Global.asax.cs中会产生奇妙的效果。它还可以防止您需要记住从特定类继承(根据Vaibhav的答案)因为它是应用程序范围内使用的。简洁,有效,完美。 - EAMann
1
https://dev59.com/D2855IYBdhLWcg3wFAHy#4548466 建议使用 app.Response.Filter.Dispose() 来释放资源,而不仅仅是将其设置为 null。 - David Keaveny

3
在我的情况下,我将其放在我的基页类中,如下所示:
public class BasePage : System.Web.UI.Page
{
    protected override void OnError(EventArgs e)
    {
        base.OnError(e);
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        if (context != null && context.Response.Filter != null) 
            context.Response.Filter = null;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接