基于ASP.NET的简单压缩

基于ASP.NET的简单压缩

web.config

1 <system.web>
2   <httpModules>
3     <add name="CompressPageHttpModule" type="WebSite.CompressPageHttpModule"/>
4   </httpModules>
5 </system.web>

类:

 1 namespace WebSite
 2 {
 3     public class CompressPageHttpModule:System.Web.IHttpModule
 4     {
 5         public void Dispose()
 6         {
 7 
 8         }
 9 
10         public void Init(System.Web.HttpApplication context)
11         {
12             context.BeginRequest += new EventHandler(Application_BeginRequest);
13         }
14 
15         private void Application_BeginRequest(object source, EventArgs e)
16         {
17             HttpContext context = ((HttpApplication)source).Context;
18 
19             if (!context.Request.UserAgent.ToLower().Contains("konqueror"))
20             {
21                 if (context.Request.Headers["Accept-encoding"] != null && context.Request.Headers["Accept-encoding"].Contains("gzip"))
22                 {
23                     context.Response.Filter = new GZipStream(context.Response.Filter,CompressionMode.Compress, true);
24                     context.Response.AppendHeader("Content-encoding", "gzip");
25                 }
26                 else if (context.Request.Headers["Accept-encoding"] != null && context.Request.Headers["Accept-encoding"].Contains("deflate"))
27                 {
28                     context.Response.Filter = new DeflateStream(context.Response.Filter,CompressionMode.Compress, true);
29                     context.Response.AppendHeader("Content-encoding", "deflate");
30                 }
31             }
32         }
33     }
34 }

 

 

 

posted @ 2012-09-18 12:38  鸿bi  阅读(135)  评论(0编辑  收藏  举报