1

我试图为我正在开发的( )网站启用压缩。我以为我一切正常,但是当我动态加载文件时,它们是通过压缩的,因此页面要么无法加载依赖于上述文件的内容,要么(在调试时)打破无数的javascript运行时异常,即;无效字符。

这是(部分)我的 Global.ascx.cs 代码后面,显然连接到 PostReleaseRequestState 事件。

void OnGlobalPostReleaseRequestState(object sender, EventArgs e)
{    
        string contentType = Response.ContentType;

        // Compress only html, style-sheet, and javascript documents.
        switch (contentType)
        {
            case "application/x-javascript":
            case "text/javascript":
            case "text/css":
            case "text/html":
                {
                    // Get the Accept-Encoding header value to know whether zipping is supported by the browser or not.
                    string acceptEncoding = Request.Headers["Accept-Encoding"];
                    if (string.IsNullOrEmpty(acceptEncoding)) return;

                    // If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
                    if (acceptEncoding.Contains("gzip"))
                    {
                        // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                        Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);
                        Response.AppendHeader("Content-Encoding", "gzip");
                    }
                    else if (acceptEncoding.Contains("deflate"))
                    {
                        // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                        Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress);
                        Response.AppendHeader("Content-Encoding", "deflate");
                    }
                }
                break;
        }
    }

还有我的 web.config 和相关部分。

<!-- language-all: lang-xml -->
<staticContent>
  <!-- Override IIS default, thus allowing JavaScript compression -->
  <remove fileExtension=".js" />
  <mimeMap fileExtension=".js" mimeType="text/javascript" />
</staticContent>
<httpCompression directory="%SystemDrive%\websites\_compressed" minFileSizeForComp="256">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="false" />
    <add mimeType="*/*" enabled="false" />
  </dynamicTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
<httpProtocol>
  <customHeaders>
    <add name="Content-Encoding" value="gzip" />
    <add name="X-UA-Compatible" value="IE=9" />
  </customHeaders>
</httpProtocol>

我需要禁用 (*.axd) 压缩,或者强制在客户端解压缩。请帮忙...

4

1 回答 1

3

好的,所以我想出了我的问题的答案......显然,我需要做的就是问这个问题,我自己来回答。首先,IE 缓存动态 (*.axd) 文件的决心很糟糕,因此我只需要清除 IE 的缓存(和历史记录)并添加一个针对请求的简单检查,如下所示。

void OnGlobalPostReleaseRequestState(object sender, EventArgs e)
{
    if (Request.RawUrl.Contains("ScriptResource.axd", StringComparison.OrdinalIgnoreCase))
    {
        return;
    }

    string contentType = Response.ContentType;

    // Compress only html, style-sheet, and javascript documents.
    switch (contentType)
    {
        case "application/x-javascript":
        case "text/javascript":
        case "text/css":
        case "text/html":
            {
                // Get the Accept-Encoding header value to know whether zipping is supported by the browser or not.
                string acceptEncoding = Request.Headers["Accept-Encoding"];
                if (string.IsNullOrEmpty(acceptEncoding)) return;

                // If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
                if (acceptEncoding.Contains("gzip"))
                {
                    // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                    Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress);
                    Response.AppendHeader("Content-Encoding", "gzip");
                }
                else if (acceptEncoding.Contains("deflate"))
                {
                    // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
                    Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress);
                    Response.AppendHeader("Content-Encoding", "deflate");
                }
            }
            break;
    }
}

如此所述 - ScriptResource.axd 被自动压缩,知道了这一点,我现在可以过滤掉所有包含作为其原始 URL 一部分的请求。

使用提琴手,我可以看到(ScriptResource.axd)文件的标题中有两个“gzip”的“Content-Encoding”,一个是自动发生的,一个是我附加的。

双重压缩 == 头疼!:)

于 2014-02-10T14:06:13.283 回答