2011-02-14 21 views
0

我正在實現HttpModule來壓縮請求。 下面是HTTP模塊的codee:HttpModule不攔截IIS 5.1中的js和css文件

public class Global : IHttpModule 
{ 
public void Init(HttpApplication app) 
{ 
    app.PostReleaseRequestState += new EventHandler(app_PostReleaseRequestState); 
} 
void app_PostReleaseRequestState(object sender, EventArgs e) 
{ 
    HttpApplication app = (HttpApplication)sender; 
    HttpContext context = app.Context; 
    string acceptEncoding = context.Request.Headers["Accept-Encoding"]; 




     // 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. 
      context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress); 
      context.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. 
      context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress); 
      context.Response.AppendHeader("Content-Encoding", "deflate"); 
     } 


} 

它能夠攔截和壓縮JS和CSS在開發Web服務器,但是當我從IIS 5.1 運行它,它是不能夠壓縮JS和CSS文件。 請幫忙。

回答

1

我會確保.js和.css文件由.NET框架處理。

的IIS 7的參考以上可以在iis.net/ConfigReference/system.webServer/handlers

關於IIS 6中找到,您可以檢查js和css正在處理: 站點設置/主頁目錄/應用程序設置/(應用程序池)配置/映射

+0

但它能攔截web開發者2008的開發web服務器。 – 2011-02-14 14:28:13

1

在IIS7之前,將非ASP.NET文件類型放入ASP.NET管道中需要設置文件類型映射以通過ISAPI重定向這些文件。如果您將*.js*.css映射爲由ISAPI處理,則您的代碼應該開始針對這些請求運行。

這裏是an example of doing that in IIS6(雖然你想用*.js*.css代替*.asp)。如果我沒有記錯的話,5.1的管理界面非常相似,ScottGu的例子應該仍然有幫助。

+0

我們是否需要爲此編寫HttpHandler或者只需要添加文件類型就可以工作。 – 2011-02-14 14:33:56

0

您在使用HttpModule的正確軌道上。但是,爲了更改HTML內容,您可以使用HttpApplication.PostReleaseRequestState處理程序。爲了更改資源文件,您可以使用HttpApplication.BeginRequest處理程序。