简体   繁体   English

ASP.NET,GZip和.AXD动态文件打得不好

[英]ASP.NET, GZip and .AXD dynamic files not playing nicely

I have feebly attempted to enable compression for an ( ) web site that I'm working on. 我试图为我正在处理的( )网站启用压缩。 I thought that I had everything working, but when I got to dynamic loading of and files, they come through compressed and as such the page either fails to load content that relies on the aforementioned files or (when debugging) breaks with countless javascript runtime exceptions, ie; 我认为我已经完成了所有工作,但是当我动态加载文件时,它们会通过压缩进行压缩,因此页面无法加载依赖于上述文件的内容或(调试时)打破了无数的javascript运行时异常,即; invalid character. 无效字符。

Here is (part of) my Global.ascx.cs code behind, obviously wired to the PostReleaseRequestState event. 这是我的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;
        }
    }

And my web.config with sections of relevance. 我的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>

I need to either disable (*.axd) compression, or force it to be decompressed on the client. 我需要禁用(* .axd)压缩,或强制它在客户端上解压缩。 Please help... 请帮忙...

Ok, so I figured out the answer to my question... Apparently asking the question is all I needed to to do, to answer it myself. 好的,所以我想出了我的问题的答案......显然问我的问题是我需要做的,自己回答。 First of all, IE sucks in terms of it's determination to cache dynamic (*.axd) files, as a result I had simply had to clear IE's cache (and history) and add a simple check against the Request as depicted below. 首先,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;
    }
}

As detailed here - ScriptResource.axd are automatically compressed, knowing this I can now filter out any and all requests that contain the as part of their Raw URL. 详见这里 -的ScriptResource.axd会自动压缩,知道了这个我现在可以过滤掉包含任何和所有请求作为其原始URL的一部分。

Using fiddler I was able to see that the (ScriptResource.axd) files were having two "Content-Encoding" of "gzip" in their headers, one that occurred automatically and one that I was appending. 使用fiddler,我能够看到(ScriptResource.axd)文件在其标题中有两个“内容编码”的“gzip”,一个是自动发生的,另一个是我追加的。

Double compression == major headaches! 双重压缩==主要的头痛! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁用ASP.Net生成的ScriptResource.axd的gzip编码 - Disable gzip encoding for ASP.Net generated ScriptResource.axd ASP.NET Web应用程序 - WebResource.axd和ScriptResource.axd文件 - 加载时间问题 - ASP.NET web application - WebResource.axd and ScriptResource.axd files - Loading time issue 如何在Asp.net中使用GZIP文件? - How to Use GZIP files In Asp.net? asp.net捆绑软件不能与.axd文件一起使用 - asp.net bundler not working with .axd file 如何组合WebResource.axd和ScriptResource.axd文件,以减少对ASP.NET服务器的请求? - How do I combine WebResource.axd and ScriptResource.axd files so as to result in less requests to my ASP.NET server? 如何在ASP.NET webforms项目中缩小和/或组合* .axd JavaScript文件? - How to minify and/or combine *.axd JavaScript files in an ASP.NET webforms project? 从ASP.NET WebResource.axd库服务器端检索Javascript文件? - Retrieving the Javascript files from an ASP.NET WebResource.axd library server side? ASP.net很好地呈现HTML(Beautify) - ASP.net render HTML nicely (Beautify) 在服务器上没有Excel的情况下在ASP.net中生成格式正确的excel文件 - generating nicely formatted excel files in ASP.net without having Excel on server 依次在Asp.Net上播放音频文件(一个接一个) - Playing Audio Files on Asp.Net in Sequence(one after one)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM