简单实现asp.net页面压缩(gzip,deflate)
虽然现在带宽上去了,但能减少一些流量总还是好的,特别是手机上网用户。
博客圆里已经有很多现成的gzip页面压缩的模块,也很好用,功能也很强;不过,正因为强了,可能需要做一些配置操作。所以如果只需要简单的对asp.net页面进行压缩的处理,可以试试下面的代码:
代码
Public Class CompressModule
Implements IHttpModule
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
AddHandler context.PreRequestHandlerExecute, AddressOf CompressModule_PreRequestHandlerExecute
End Sub
Protected Sub CompressModule_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
Dim app As HttpApplication = CType(sender, HttpApplication)
'检测是否为ASP.NET页面,是否MS-AJAX应用
If TypeOf app.Context.CurrentHandler Is UI.Page AndAlso app.Request.ServerVariables("HTTP_X_MICROSOFTAJAX") Is Nothing Then
Dim type As CompressType = Me.GetCompressType(app)
If type = CompressType.GZip Then
app.Response.Filter = New GZipStream(app.Response.Filter, CompressionMode.Compress)
app.Response.AppendHeader("Content-Encoding", type.ToString().ToLower())
ElseIf type = CompressType.Deflate Then
app.Response.Filter = New DeflateStream(app.Response.Filter, CompressionMode.Compress)
app.Response.AppendHeader("Content-Encoding", type.ToString().ToLower())
End If
End If
End Sub
''' <summary>
''' 获取客户端支持的压缩类型
''' </summary>
''' <param name="context">The context.</param>
''' <returns></returns>
Protected Function GetCompressType(ByVal context As HttpApplication) As CompressType
Dim accept As String = context.Request.Headers("Accept-Encoding")
If String.IsNullOrEmpty(accept) Then
Return CompressType.None
End If
accept = accept.ToUpperInvariant()
If accept.Contains("GZIP") Then
Return CompressType.GZip
End If
If accept.Contains("DEFLATE") Then
Return CompressType.Deflate
End If
Return CompressType.None
End Function
''' <summary>
''' 压缩类型枚举
''' </summary>
Protected Enum CompressType
''' <summary>
''' GZIP压缩
''' </summary>
GZip
''' <summary>
''' Deflate压缩
''' </summary>
Deflate
''' <summary>
''' 不支持压缩
''' </summary>
None
End Enum
End Class
Implements IHttpModule
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
AddHandler context.PreRequestHandlerExecute, AddressOf CompressModule_PreRequestHandlerExecute
End Sub
Protected Sub CompressModule_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
Dim app As HttpApplication = CType(sender, HttpApplication)
'检测是否为ASP.NET页面,是否MS-AJAX应用
If TypeOf app.Context.CurrentHandler Is UI.Page AndAlso app.Request.ServerVariables("HTTP_X_MICROSOFTAJAX") Is Nothing Then
Dim type As CompressType = Me.GetCompressType(app)
If type = CompressType.GZip Then
app.Response.Filter = New GZipStream(app.Response.Filter, CompressionMode.Compress)
app.Response.AppendHeader("Content-Encoding", type.ToString().ToLower())
ElseIf type = CompressType.Deflate Then
app.Response.Filter = New DeflateStream(app.Response.Filter, CompressionMode.Compress)
app.Response.AppendHeader("Content-Encoding", type.ToString().ToLower())
End If
End If
End Sub
''' <summary>
''' 获取客户端支持的压缩类型
''' </summary>
''' <param name="context">The context.</param>
''' <returns></returns>
Protected Function GetCompressType(ByVal context As HttpApplication) As CompressType
Dim accept As String = context.Request.Headers("Accept-Encoding")
If String.IsNullOrEmpty(accept) Then
Return CompressType.None
End If
accept = accept.ToUpperInvariant()
If accept.Contains("GZIP") Then
Return CompressType.GZip
End If
If accept.Contains("DEFLATE") Then
Return CompressType.Deflate
End If
Return CompressType.None
End Function
''' <summary>
''' 压缩类型枚举
''' </summary>
Protected Enum CompressType
''' <summary>
''' GZIP压缩
''' </summary>
GZip
''' <summary>
''' Deflate压缩
''' </summary>
Deflate
''' <summary>
''' 不支持压缩
''' </summary>
None
End Enum
End Class
编译后,并在web.config文件中添加httpmodule模块配置内容就OK了。
当然也可能将以上代码作出一些修改后直接添加到您的页面基类里使用。