【问题标题】:ASP.NET, GZip and .AXD dynamic files not playing nicelyASP.NET、GZip 和 .AXD 动态文件无法正常播放
【发布时间】:2014-02-07 14:15:08
【问题描述】:

我试图为我正在开发的 () 网站启用 压缩。我以为我一切正常,但是当我动态加载 和 文件时,它们是通过压缩的,因此页面要么无法加载依赖于上述文件的内容,要么(在调试时)与无数 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) 压缩,或者强制在客户端上对其进行解压缩。请帮忙...

【问题讨论】:

    标签: gzip asp.net webforms webresource.axd scriptresource.axd asp.net gzip webresource.axd scriptresource.axd


    【解决方案1】:

    好的,所以我找到了我的问题的答案......显然,我需要做的就是提出这个问题,我自己来回答。首先,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;
        }
    }
    

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

    使用 fiddler 我可以看到 (ScriptResource.axd) 文件的标题中有两个“gzip”的“内容编码”,一个是自动发生的,一个是我附加的。

    双重压缩 == 头疼! :)

    【讨论】:

    • 这是我完全相同的问题。启用 gzip 后,ScriptResource 响应被双重 gzip 压缩。您的解决方案效果很好。
    • @DavidPine 我发现无论是 .css 还是 .axd 都无法提供 gzip 压缩资源。更令人沮丧的是,当我查看 IIS 压缩文件夹时,我看到那里的文件已压缩,但我在响应标头中没有看到 Contact Encoding: GZip。在上面的代码中,您手动添加了此标头...为什么? IIS 不会自动执行此操作吗?并且,您的 GlobalAsax 代码是否需要启用 AXD 资源压缩,即您不能通过 web.config 执行此操作
    • @Jacques,我真的不记得了。这是很久以前的事了,细节已经从我的记忆中消失了。我建议您将此作为一个新问题提出,并随时在您的新问题中引用此 Q/A。抱歉,我无法提供更多帮助。
    • @DavidPine 谢谢大卫。我有一个问题在这里运行stackoverflow.com/q/38250376/392591,我正在尝试各种解决方案。
    猜你喜欢
    • 2017-04-02
    • 2021-09-13
    • 2019-04-29
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 2017-12-28
    相关资源
    最近更新 更多