【问题标题】:Can a page opt out of IIS 7 compression?页面可以选择退出 IIS 7 压缩吗?
【发布时间】:2010-04-20 19:33:04
【问题描述】:

IIS7 使用 GZIP 自动压缩我的页面。

太好了...但是,对于一个特定页面,我需要在需要时使用Response.Flush() 将其流式传输给用户。但是当输出被压缩时,IIS 服务器似乎会收集我所有的输出,直到页面完成,然后再压缩并将其发送到客户端。这使我尝试将内容刷新给用户的尝试无效。

有没有办法让这一页退出压缩?

一种可能的选择

我已经确定,如果我手动将内容类型设置为与c:\windows\system32\inetsrv\config\applicationhost.config 处的 IIS 配置不匹配的内容类型,那么 IIS 将不会对其进行压缩。例如。 Response.ContentType = "x-text/html"。这适用于 IE8,因为它回退到显示 HTML。但 Firefox 会询问用户如何处理未知文件类型。

如果有另一个我可以使用的 Mime 类型,浏览器会接受它作为 HTML,这可能会起作用,这在 applicationhost.config 中是不匹配的。作为参考,这些是将被压缩的 mime 类型:

   <add mimeType="text/*" enabled="true" />
   <add mimeType="message/*" enabled="true" />
   <add mimeType="application/x-javascript" enabled="true" />
   <add mimeType="application/atom+xml" enabled="true" />
   <add mimeType="application/xaml+xml" enabled="true" />

其他选项?

还有其他选择退出压缩的选项吗?

【问题讨论】:

    标签: c# asp.net iis compression iis-7.5


    【解决方案1】:

    可能无法禁用特定页面的压缩,但您可以禁用目录。

    这描述了如何禁用静态压缩,但它可能适用于动态压缩:(来自http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/502ef631-3695-4616-b268-cbe7cf1351ce.mspx?mfr=true

    要仅对单个目录禁用静态压缩,请先启用全局静态压缩(如果已禁用),然后在该目录禁用静态压缩。例如,要为http://www.contoso.com/Home/StyleSheets 的目录启用静态压缩,请执行以下步骤:

    1. 通过在命令提示符处执行以下命令来启用全局静态压缩:

    adsutil set w3svc/filters/compression/parameters/HcDoStaticCompression true

    1. 通过在命令提示符处执行以下命令来禁用此目录的静态压缩:

    adsutil set w3svc/1/root/Home/StyleSheets/DoStaticCompression false

    【讨论】:

      【解决方案2】:

      不确定我是否喜欢这个,但也许值得一提: Disable GZIP compression for IE6 clients

      【讨论】:

      • 这是迄今为止我见过的最有前途的方法。谢谢!
      【解决方案3】:

      您可以使用定制的压缩模块,例如:

      HTTP compression of WebResource.axd and pages in ASP.NET

      使用它应该很容易自定义要包含/排除的文件。

      【讨论】:

        【解决方案4】:

        我知道页面在请求期间无法以编程方式自行禁用。但是,您可以解决压缩问题并发送一些额外的填充垃圾,足以让 gzip 处理新块。您的填充数据应尽可能随机,以免压缩过大,从而更快地填充 deflate 缓冲区。

        实际发送的数据量取决于压缩模块的配置。

        【讨论】:

          【解决方案5】:

          如果您执行 Response.BufferOutput = false ,它将停止内置压缩工作,尽管不是很干净。您可能会收到事件警告,指出在已将标头发送到客户端后无法添加标头。

          【讨论】:

            【解决方案6】:

            如果你需要一个只依赖于 C# 的解决方案,你可以修改我写的这个方法来解决 Android 浏览器中的问题:

            /// <summary>
            /// Alters the current HTTP request only for Android user agents, in order to disable web page compression so the Android browser will not cut off most of the page content, based on the Content-length HTTP header. 
            /// </summary>
            public static void fixAndroidPageDisplay()
            {
                HttpContext c = HttpContext.Current;
                if (c == null) return;
                HttpRequest r = c.Request;
                if (r == null || r.UserAgent == null) return;
                if (r.UserAgent.ToLowerInvariant().Contains("android"))
                {
                    HttpResponse rsp = c.Response;
            
                    if (rsp != null)
                    {
                        string ce = null;
                        foreach (string s in rsp.Headers.Keys)
                        {
                            if (s != null)
                            {
                                if (s.ToLowerInvariant().Equals("content-encoding")) {
                                    ce = s;
                                }
                            }
                        }
                        if (ce != null) {
                            rsp.Headers[ce] = "text/html";
                            rsp.Filter = rsp.OutputStream;
                        }
                    }
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2011-08-05
              • 2011-04-30
              • 2011-04-14
              • 2010-10-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-05-21
              相关资源
              最近更新 更多