2014-02-07 29 views
1

我试图为我正在处理的()网站启用压缩。我认为我有一切工作,但是当我动态加载文件时,它们通过压缩来实现,因此页面无法加载依赖于上述文件的内容或(调试时)无数javascript中断的内容运行时异常,即;无效字符。ASP.NET,GZip和.AXD动态文件播放不好

这里是我的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)压缩,或强制它在客户端解压缩。请帮助...

回答

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; 
    } 
} 

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

使用fiddler我能够看到(ScriptResource.axd)文件在它们的头文件中有两个“gzip”的“Content-Encoding”,一个是自动发生的,一个是我正在追加的。

双重压缩==主要头痛! :)

+0

这是我完全一样的问题。启用gzip后,ScriptResource响应被双重压缩。您的解决方案很好。 – rocketsarefast

+0

@DavidPine无论是.css还是.axd,我都发现无法提供gzip压缩资源。更令人沮丧的是,当我查看IIS压缩文件夹时,我看到压缩的文件,但在响应头文件中没有接触编码:GZip。在你的代码中,你正在手动添加这个头......为什么? IIS不会自动执行该操作吗? AND,上面是您的GlobalAsax代码需要启用压缩的AXD资源,即你不能通过web.config – Jacques

+0

@Jacques,我真的不记得。这是很久以前的事了,细节已经逃离了我的回忆。我建议你把这个问题作为一个新问题,并且随时在你的新问题中引用这个Q/A。对不起,我无法提供更多帮助。 –