2010-01-22 81 views
0

我使用了YSlow Firefox附加組件,它返回以下結果:使用gzip壓縮組件的D級

--------------------- -------------------------------------------------- ------------------
d級上壓縮部件用gzip

有應發送壓縮

* http://localhost:63808/WebSite/BemVindo/ 
* http://localhost:63808/WebSite/css/Global.css?... 
* http://localhost:63808/WebSite/js/Global.js?... 

3個明文部件 - -------------------------------------------------- --------------------------------------
於是我開始尋找並得到了這一段代碼:

的Global.asax

Private Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs) 

    Dim Response As HttpResponse = HttpContext.Current.Response 
    Dim AcceptEncoding As String = HttpContext.Current.Request.Headers("Accept-Encoding") 

    If Not String.IsNullOrEmpty(AcceptEncoding) AndAlso AcceptEncoding.Contains("gzip") OrElse AcceptEncoding.Contains("deflate") Then 
     If AcceptEncoding.Contains("deflate") Then 
      Response.Filter = New System.IO.Compression.DeflateStream(Response.Filter, System.IO.Compression.CompressionMode.Compress) 
      Response.AppendHeader("Content-Encoding", "deflate") 
     Else 
      Response.Filter = New System.IO.Compression.GZipStream(Response.Filter, System.IO.Compression.CompressionMode.Compress) 
      Response.AppendHeader("Content-Encoding", "gzip") 
     End If 
    End If 

    Response.AppendHeader("Vary", "Content-Encoding") 

End Sub 

現在我有:

上用gzip

精細壓縮部件A級,哼?問題是:Application_PreRequestHandlerExecute事件是對請求進行gzip/deflate的最佳位置嗎?

+0

我沒有直接回答,因爲我沒有做IIS,所以我可能是錯的「最佳實踐」,但你通常* *配置此在這裏描述的Web服務器級別http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/25d2170b-09c0-45fd-8da4-898cf9a7d568.mspx?mfr=true順便說一下,當前標題「添加過期標題」相關的問題? :) – BalusC 2010-01-22 21:21:44

+0

但我沒有控制IIS ......這段代碼實際上起作用了,但我擔心它是否會在請求開銷之前! 呵呵,我改了標題,這是另外一個問題。 – Fernando 2010-01-22 21:40:29

回答

1

是的,Application_PreRequestHandlerExecute事件是對請求進行gzip/deflate的最佳位置。

2

不確定,但您的代碼可能會導致使用Ajax的頁面出現問題。下面的頁面顯示了類似的代碼,但如果檢測到Ajax,則取消壓縮:Enabling Gzip and Deflate HTTP Compression in ASP.NET pages。我在VB中翻譯了代碼,但沒有嘗試過。

埃裏克

Private Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs) 
Dim app As HttpApplication = TryCast(sender, HttpApplication) 
Dim acceptEncoding As String = app.Request.Headers("Accept-Encoding") 
Dim prevUncompressedStream As Stream = app.Response.Filter 

If Not (TypeOf app.Context.CurrentHandler Is Page OrElse app.Context.CurrentHandler.[GetType]().Name = "SyncSessionlessHandler") OrElse app.Request("HTTP_X_MICROSOFTAJAX") IsNot Nothing Then 
    Return 
End If 

If acceptEncoding Is Nothing OrElse acceptEncoding.Length = 0 Then 
    Return 
End If 

acceptEncoding = acceptEncoding.ToLower() 

If acceptEncoding.Contains("deflate") OrElse acceptEncoding = "*" Then 
    ' defalte 
    app.Response.Filter = New DeflateStream(prevUncompressedStream, CompressionMode.Compress) 
    app.Response.AppendHeader("Content-Encoding", "deflate") 
ElseIf acceptEncoding.Contains("gzip") Then 
    ' gzip 
    app.Response.Filter = New GZipStream(prevUncompressedStream, CompressionMode.Compress) 
    app.Response.AppendHeader("Content-Encoding", "gzip") 
End If 

末次