• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

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

    來源:懂視網 責編:小采 時間:2020-11-27 22:42:44
    文檔

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

    Asp.net使用HttpModule壓縮并刪除空白Html請求的實現代碼:同時我們還可以刪除一些空白 段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來實現壓縮與刪除空白類, 繼承自Stream類: 代碼如下:/// <summary> /// CompressWhitespaceFilter /// </summary> public c
    推薦度:
    導讀Asp.net使用HttpModule壓縮并刪除空白Html請求的實現代碼:同時我們還可以刪除一些空白 段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來實現壓縮與刪除空白類, 繼承自Stream類: 代碼如下:/// <summary> /// CompressWhitespaceFilter /// </summary> public c

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

    /// <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
    }

    上面的代碼使用正則表達式替換字符串,你可以修改那些正則表達式來滿足你的需求. 我們同時使用了GZipStream與DeflateStream實現了壓縮. 好的,接下來與
    HttpModule結合:
    代碼如下:

    /// <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 響應請求時作為 HTTP 執行管線鏈中的第一個事件發生。
    在WEB.CONFIG中你還需要配置:
    代碼如下:

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

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

    OringinalTraffice

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

    GZIPCompession

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

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

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

    Asp.net使用HttpModule壓縮并刪除空白Html請求的實現代碼:同時我們還可以刪除一些空白 段,空行,注釋等以使得HTML文檔的尺寸變得更小. 讓我們先來實現壓縮與刪除空白類, 繼承自Stream類: 代碼如下:/// <summary> /// CompressWhitespaceFilter /// </summary> public c
    推薦度:
    標簽: 請求 空白 壓縮
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产精品白丝AV嫩草影院| 久久国产精品偷99| 免费人妻精品一区二区三区| 国产福利在线观看精品| 亚洲国产精品一区二区久久hs| 国产精品99久久久久久猫咪| 久久精品www人人爽人人| 国产午夜精品一区二区三区小说| 在线成人精品国产区免费| 四虎成人精品免费影院| 日韩精品无码一区二区三区不卡| 久久精品国产亚洲av影院 | 欧美成人精品网站播放 | 国产国产精品人在线视| 亚洲国产精品人人做人人爱| 国产精品成人无码久久久久久 | 午夜精品久久久久久影视777| 国产亚洲精品精品国产亚洲综合| 久久精品成人国产午夜| 99久久成人国产精品免费| 色欲精品国产一区二区三区AV| 免费视频成人国产精品网站| 精品国产不卡一区二区三区 | 97精品一区二区视频在线观看 | 欧美日韩国产精品| 国产精品成人无码久久久久久| 亚洲国产精品婷婷久久| 久久免费国产精品一区二区| 精品一区二区三区中文字幕| 精品成人免费自拍视频| 久久久久免费精品国产| 日韩精品一区二区三区大桥未久| 久久最新精品国产| 中文字幕亚洲综合精品一区| 亚洲国产精品自在线一区二区 | 精品91自产拍在线观看| 精品无码国产自产拍在线观看蜜| 国精品产露脸自拍| 久久精品免费网站网| 无码国内精品久久人妻麻豆按摩 | 亚洲国产精品免费视频|