ASP.NET頁面壓縮 Compression Gzip Deflate

来源:互联网 发布:泰晤士河报软件 编辑:程序博客网 时间:2024/06/11 20:09

稍微研究了一下頁面壓縮...

壓縮後的頁面空間變小,可以稍微的降低網頁的流量,提昇傳輸的速度...

廢話不多說 來看程式囉

view plaincopy to clipboardprint?
  1. using System;  
  2. using System.IO;  
  3. using System.IO.Compression;  
  4. using System.Web;  
  5.   
  6. namespace Library.framework  
  7. {  
  8.     /// <!--HTTP壓縮輸出-->  
  9.     /// <summary>  
  10.     /// HTTP壓縮輸出 - design By Phoenix 2008 -  
  11.     /// </summary>  
  12.     /// Update:2008.09.08  
  13.     /// Developer:Phoenix  
  14.     /// Version:1.0.0918   
  15.     public class HttpCompression:IHttpModule  
  16.     {  
  17.  
  18.         #region IHttpModule 成員  
  19.   
  20.         void IHttpModule.Dispose()  
  21.         {  
  22.             //Dispose  
  23.         }  
  24.   
  25.         void IHttpModule.Init(HttpApplication context)  
  26.         {  
  27.             //加入壓縮事件  
  28.             context.BeginRequest += new EventHandler(context_BeginRequest);  
  29.         }  
  30.  
  31.         #endregion  
  32.   
  33.         void context_BeginRequest(object sender, EventArgs e)  
  34.         {  
  35.             //提取Application  
  36.             HttpApplication app = (HttpApplication)sender;  
  37.             //判斷是否為*.axd (此檔不可壓縮)  
  38.             if (app.Request.Path.Contains("axd"))  
  39.                 return;  
  40.             //提取支援的壓縮格式  
  41.             string encodiongs = app.Request.Headers.Get("Accept-Encoding");  
  42.             //判斷是否支援壓縮  
  43.             if (string.IsNullOrEmpty(encodiongs))  
  44.             {  
  45.                 app.Context.Items["Compression"] = "Compression disabled";  
  46.                 return;  
  47.             }  
  48.             //提取輸出資料流  
  49.             Stream Filter = app.Response.Filter;  
  50.             //將壓縮格式轉小寫  
  51.             encodiongs = encodiongs.ToLower();  
  52.             //判斷是否支援Gzip  
  53.             if (encodiongs.Contains("gzip"))  
  54.             {  
  55.                 //將壓縮過的資料流取代原本的資料流  
  56.                 app.Response.Filter = new GZipStream(Filter, CompressionMode.Compress);  
  57.                 //將壓縮格式加入至標頭  
  58.                 app.Response.AppendHeader("Content-Encoding""gzip");  
  59.                 //將壓縮格式寫入事件  
  60.                 app.Context.Trace.Warn("Gzip enabled");  
  61.                 app.Context.Items["Compression"] = "Gzip enabled";  
  62.             }  
  63.             else  
  64.             {  
  65.                 //將壓縮過的資料流取代原本的資料流  
  66.                 app.Response.Filter = new DeflateStream(Filter, CompressionMode.Compress);  
  67.                 //將壓縮格式加入至標頭  
  68.                 app.Response.AppendHeader("Content-Encoding""deflate");  
  69.                 //將壓縮格式寫入事件  
  70.                 app.Context.Trace.Warn("Deflate enabled");  
  71.                 app.Context.Items["Compression"] = "Deflate enabled";  
  72.             }  
  73.         }  
  74.     }  
  75. }  

基本上 ... ScriptResource.axd 這個檔案是不可以壓縮的(不然後果嚴重) 瀏覽器不會自己解壓縮

所以我把這個檔案排除

再來最重要的 是在Web.config 內將其加入至httpModules標籤內

<add name="HttpCompression" type="Library.framework.HttpCompression"/>

再來一個 可以在頁面底部標示啟動哪種壓縮模式(請自行加入文件底部)

<%=Context.Items["Compression"].ToString() %>

以上