ASP.NET頁面壓縮 Compression Gzip Deflate

本文介绍了一种通过GZIP和Deflate算法实现的网页压缩方法,能够有效减少页面大小,提升加载速度。文章提供了一个.NET实现的具体示例,并说明了如何配置Web.config启用压缩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

廢話不多說 來看程式囉

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

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

所以我把這個檔案排除

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

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

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

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

以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值