CompressionModule,让ASP.NET支持Gzip压缩输出

4 篇文章 0 订阅
3 篇文章 0 订阅

文章转自:爱符号  http://www.afuhao.com/article_articleId-132.shtml


你的网站如果是asp.net开发的,有没有考虑压缩输出内容?

有的人知道IIS是支持压缩输出的,但是有的时候后台有的页面是动态输出的,如果开启了动态压缩,就会导致要动态输出完成后,才会看到页面结果

所以这个时候就会有一个纠结的问题,如果开启了动态压缩,那么动态输出的页面就会很慢。

但是不开启压缩,又会浪费很多传输资源。


那么当你发现别的都帮不了你的时候,自己才是最靠得住的!

声明一下:这个模块的源代码我直接找的,然后优化一些地方,如果你曾经看到过,也不用奇怪。可能你会问怎么不自己写,其实这种只要没毛病又效率高的现成代码何必自己写呢。


CompressionModule.cs

[cs] 
  1. namespace Symbol.Web {  
  2.     using System;  
  3.     using System.IO.Compression;  
  4.     using System.Web;  
  5.   
  6.     public class CompressionModule : IHttpModule {  
  7.         void IHttpModule.Dispose() { }  
  8.   
  9.         void IHttpModule.Init(HttpApplication context) {  
  10.             context.PreRequestHandlerExecute += new EventHandler(context_PostReleaseRequestState);  
  11.         }  
  12.   
  13.         void context_PostReleaseRequestState(object sender, EventArgs e) {  
  14.             HttpApplication app = (HttpApplication)sender;  
  15.             //这里做一个过滤,后台不需要压缩,具体可以自己写一些判断,  
  16.             //我这里只是当访问路径有 /area开头时不允许压缩,因为后台有时候是动态输出,用了压缩就没有意义了  
  17.             if (app.Request.Path.StartsWith("/area/", StringComparison.OrdinalIgnoreCase))  
  18.                 return;  
  19.             //当前为aspx页面,并且不是WEBFORM回调时,就开启压缩  
  20.             if (app.Context.CurrentHandler is System.Web.UI.Page && app.Request["HTTP_X_MICROSOFTAJAX"] == null) {  
  21.                 if (IsEncodingAccepted(GZIP)) {//优先使用GZIP压缩  
  22.                     app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);  
  23.                     SetEncoding(GZIP);  
  24.                 } else if (IsEncodingAccepted(DEFLATE)) {//Deflate也可以  
  25.                     app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);  
  26.                     SetEncoding(DEFLATE);  
  27.                 }  
  28.             }  
  29.         }  
  30.   
  31.         private static readonly string GZIP = "gzip";  
  32.         private static readonly string DEFLATE = "deflate";  
  33.         //判断客户端是否支持指定的压缩  
  34.         private static bool IsEncodingAccepted(string encoding) {  
  35.             HttpContext context = HttpContext.Current;  
  36.             return context.Request.Headers["Accept-Encoding"] != null && context.Request.Headers["Accept-encoding"].Contains(encoding);  
  37.         }  
  38.         //开启了压缩后,需要设置一下返回的编码类型,方便客户端解压缩  
  39.         private static void SetEncoding(string encoding) {  
  40.             HttpContext.Current.Response.AppendHeader("Content-Encoding", encoding);  
  41.         }  
  42.     }  
  43. }  


web.config配置


[xml]
  1. <!--这是放在<system.web>节点的,iis5,iis6读取这里的-->  
  2.     <httpModules>  
  3.       <add name="CompressionModule" type="Symbol.Web.CompressionModule, www.afuhao.com"/><!--www.afuhao.com是你的程序集名称-->  
  4.     </httpModules>  
  5.   
  6.   
  7. <!--这是放到<system.webServer>节点的,iis7读取这里的-->  
  8.     <modules runAllManagedModulesForAllRequests="true">  
  9.       <add name="CompressionModule" type="Symbol.Web.CompressionModule, www.afuhao.com"/><!--www.afuhao.com是你的程序集名称-->  
  10.     </modules>  
要是不会配置,就跟贴了



网页GZIP压缩检测 http://tool.chinaz.com/Gzips/


下面是效果图:

首页检查,已经压缩了


注册页面 /area开头,没有压缩



文章转自:爱符号  http://www.afuhao.com/article_articleId-132.shtml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值