I am using code, similar to the code in this question:
http://stackoverflow.com/questions/1047878/how-to-make-webpage-with-gzip/1048066#1048066
But I noticed that once I started using Server.Transfer calls, Firefox would report that the page was not encoded properly. So I did some searching and found that putting the code in PreRequestHandlerExecute, rather than BeginRequest made Server.Transfer calls work correctly again.
I am having a hard time finding solid info about the difference. Is one better than the other? Is there just a bug in my code that I should fix to use BeginRequest? Is there something wrong with PreRequestHandlerExecute?
EDIT: I just realized that the BeginRequest/Server.Transfer combo worked fine in IIS 5.1 on WinXP and IIS6 on Win2k3. But now that I am on Win7 with IIS7, it is broken. What's going on here?
EDIT: I just confirmed the above. If I put the ASP.NET app in an app pool with Classic Pipeline mode, it works fine with BeginRequest + Server.Transfer. Switching it back to Integrated Pipeline mode breaks that combo.
Here's my code from global.asax.vb:
Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
If m_CompressionSettings IsNot Nothing AndAlso m_CompressionSettings.Enabled Then
AddCompressionFilter(DirectCast(sender, HttpApplication), m_CompressionSettings)
End If
End Sub
Private Shared Sub AddCompressionFilter(ByVal app As HttpApplication, ByVal compressionFilterConfiguration As CompressionFilterSection)
Dim url As String = app.Request.Url.ToString().ToLower()
For Each item As ExcludeElement In compressionFilterConfiguration.Excludes
If url.Contains(item.Name.ToLower()) Then
Return
End If
Next
Dim acceptEncoding As String = app.Request.Headers("Accept-Encoding")
Dim prevUncompressedStream As Stream = app.Response.Filter
If String.IsNullOrEmpty(acceptEncoding) Then
Return
End If
acceptEncoding = acceptEncoding.ToLower()
If (acceptEncoding.Contains("gzip")) Then
'gzip
app.Response.Filter = New GZipStream(prevUncompressedStream, CompressionMode.Compress)
app.Response.AppendHeader("Content-Encoding", "gzip")
ElseIf (acceptEncoding.Contains("deflate")) Then
'deflate
app.Response.Filter = New DeflateStream(prevUncompressedStream, CompressionMode.Compress)
app.Response.AppendHeader("Content-Encoding", "deflate")
End If
End Sub