一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - ASP.NET教程 - Asp.net使用HttpModule壓縮并刪除空白Html請求的實現(xiàn)代碼

Asp.net使用HttpModule壓縮并刪除空白Html請求的實現(xiàn)代碼

2019-09-23 10:39asp.net教程網(wǎng) ASP.NET教程

當(dāng)我們壓縮我的Response后再傳到Client端時,可以明顯節(jié)省寬帶. 提升Site的性能. 現(xiàn)在的瀏覽器大部分都支持Gzip,Deflate壓縮

同時我們還可以刪除一些空白 
段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來實現(xiàn)壓縮與刪除空白類, 繼承自Stream類: 

復(fù)制代碼代碼如下:


/// <summary> 
/// CompressWhitespaceFilter 
/// </summary> 
public class CompressWhitespaceFilter : Stream 

private GZipStream _contentGZipStream; 
private DeflateStream _content_DeflateStream; 
private Stream _contentStream; 
private CompressOptions _compressOptions; 
/// <summary> 
/// Initializes a new instance of the <see cref="CompressWhitespaceFilter"/> class. 
/// </summary> 
/// <param name="contentStream">The content stream.</param> 
/// <param name="compressOptions">The compress options.</param> 
public CompressWhitespaceFilter(Stream contentStream, CompressOptions compressOptions) 

if (compressOptions == CompressOptions.GZip) 

this._contentGZipStream = new GZipStream(contentStream, CompressionMode.Compress); 
this._contentStream = this._contentGZipStream; 

else if (compressOptions == CompressOptions.Deflate) 

this._content_DeflateStream = new DeflateStream(contentStream,CompressionMode.Compress); 
this._contentStream = this._content_DeflateStream; 

else 

this._contentStream = contentStream; 

this._compressOptions = compressOptions; 

public override bool CanRead 

get { return this._contentStream.CanRead; } 

public override bool CanSeek 

get { return this._contentStream.CanSeek; } 

public override bool CanWrite 

get { return this._contentStream.CanWrite; } 

public override void Flush() 

this._contentStream.Flush(); 

public override long Length 

get { return this._contentStream.Length; } 

public override long Position 

get 

return this._contentStream.Position; 

set 

this._contentStream.Position = value; 


public override int Read(byte[] buffer, int offset, int count) 

return this._contentStream.Read(buffer, offset, count); 

public override long Seek(long offset, SeekOrigin origin) 

return this._contentStream.Seek(offset, origin); 

public override void SetLength(long value) 

this._contentStream.SetLength(value); 

public override void Write(byte[] buffer, int offset, int count) 

byte[] data = new byte[count + 1]; 
Buffer.BlockCopy(buffer, offset, data, 0, count); 
string strtext = System.Text.Encoding.UTF8.GetString(data); 
strtext = Regex.Replace(strtext, "^\s*", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); 
strtext = Regex.Replace(strtext, "\r\n", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); 
strtext = Regex.Replace(strtext, "<!--*.*?-->", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline); 
byte[] outdata = System.Text.Encoding.UTF8.GetBytes(strtext); 
this._contentStream.Write(outdata, 0, outdata.GetLength(0)); 


/// <summary> 
/// CompressOptions 
/// </summary> 
/// <seealso cref="http://en.wikipedia.org/wiki/Zcat#gunzip_and_zcat"/> 
/// <seealso cref="http://en.wikipedia.org/wiki/DEFLATE"/> 
public enum CompressOptions 

GZip, 
Deflate, 
None 


上面的代碼使用正則表達(dá)式替換字符串,你可以修改那些正則表達(dá)式來滿足你的需求. 我們同時使用了GZipStreamDeflateStream實現(xiàn)了壓縮. 好的,接下來與
HttpModule結(jié)合:

復(fù)制代碼代碼如下:


/// <summary> 
/// CompressWhitespaceModule 
/// </summary> 
public class CompressWhitespaceModule : IHttpModule 

#region IHttpModule Members 
/// <summary> 
/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>. 
/// </summary> 
public void Dispose() 

// Nothing to dispose; 

/// <summary> 
/// Initializes a module and prepares it to handle requests. 
/// </summary> 
/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param> 
public void Init(HttpApplication context) 

context.BeginRequest += new EventHandler(context_BeginRequest); 

/// <summary> 
/// Handles the BeginRequest event of the context control. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 
void context_BeginRequest(object sender, EventArgs e) 

HttpApplication app = sender as HttpApplication; 
if (app.Request.RawUrl.Contains(".aspx")) 

HttpContext context = app.Context; 
HttpRequest request = context.Request; 
string acceptEncoding = request.Headers["Accept-Encoding"]; 
HttpResponse response = context.Response; 
if (!string.IsNullOrEmpty(acceptEncoding)) 

acceptEncoding = acceptEncoding.ToUpperInvariant(); 
if (acceptEncoding.Contains("GZIP")) 

response.Filter = new CompressWhitespaceFilter(context.Response.Filter, CompressOptions.GZip); 
response.AppendHeader("Content-encoding", "gzip"); 

else if (acceptEncoding.Contains("DEFLATE")) 

response.Filter = new CompressWhitespaceFilter(context.Response.Filter, CompressOptions.Deflate); 
response.AppendHeader("Content-encoding", "deflate"); 


response.Cache.VaryByHeaders["Accept-Encoding"] = true; 


#endregion 


HttpApplication.BeginRequest 事件是 在 ASP.NET 響應(yīng)請求時作為 HTTP 執(zhí)行管線鏈中的第一個事件發(fā)生。 
在WEB.CONFIG中你還需要配置: 

復(fù)制代碼代碼如下:


<httpModules> 
<add name="CompressWhitespaceModule" type="MyWeb.CompressWhitespaceModule" /> 
</httpModules> 

 

我們來看一下效果,下面沒有使用時, 4.8KB

Asp.net使用HttpModule壓縮并刪除空白Html請求的實現(xiàn)代碼

接著看,處理過后的效果,Cotent-Encoding: gzip,  filezie: 1.6KB

Asp.net使用HttpModule壓縮并刪除空白Html請求的實現(xiàn)代碼

很簡單,你可以按需求來增加更多的功能. 希望對您開發(fā)有幫助.
作者:Petter Liu

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品资源在线观看网站 | 国产99视频精品免视看9 | 人人擦 | 亚洲第成色999久久网站 | 26uuu老色哥 236宅宅2021最新理论 | 99在线观看免费视频 | a v在线男人的天堂观看免费 | 日韩免费一级毛片 | 国产在线视频自拍 | 精品久久香蕉国产线看观看亚洲 | 日本免费全黄一级裸片视频 | 日本亚洲欧洲高清有码在线播放 | 免费高清在线观看 | 婷婷在线成人免费观看搜索 | 无遮挡免费h肉动漫在线观看 | 久久精品一区二区三区资源网 | 91交换论坛| 爱爱一级视频 | 性生大片免费看 | 韩国三级日本三级香港三级黄 | 欧美 变态 另类 人妖班 | 欧美影院一区二区三区 | 欧美亚洲欧美 | 国产专区亚洲欧美另类在线 | 湿好紧太硬了我太爽了 | 蜜柚精彩在线观看 | 成年人免费观看的视频 | 色多多影院 | 亚洲色图综合网 | 九九九九九九精品免费 | 俄罗斯bbbbbbbbb大片 | 欧美成人免费观看国产 | 日本一二线不卡在线观看 | 天天视频官网天天视频在线 | 四虎精品在线观看 | 好大好猛好爽好深视频免费 | 吃胸膜奶视频456 | 亚洲精品综合网 | 成人在线一区二区 | 91麻豆精品国产自产在线 | 91制片厂果冻传媒杨柳作品 |