<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答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
        主站蜘蛛池模板: 亚洲春色在线视频| 久久久久久亚洲精品不卡| 亚洲国产一区国产亚洲| 国产真人无码作爱免费视频| 国产精品亚洲w码日韩中文| 深夜特黄a级毛片免费播放| 又黄又爽的视频免费看| 妇女自拍偷自拍亚洲精品| 亚洲精品福利你懂| AA免费观看的1000部电影| 亚洲男人的天堂久久精品| 午夜色a大片在线观看免费| 美女裸体无遮挡免费视频网站| 免费国产成人高清在线观看麻豆| 色偷偷噜噜噜亚洲男人| 91短视频在线免费观看| 久久综合亚洲色HEZYO社区| 国产免费丝袜调教视频| 亚洲欧美日韩中文二区| 啊v在线免费观看| 久久精品成人免费观看97| 四虎影视在线永久免费观看| 老司机午夜精品视频在线观看免费 | 国产人在线成免费视频| 亚洲中文字幕无码中文字在线| 精品一区二区三区免费视频| 亚洲天堂男人天堂| 大学生a级毛片免费观看| 色妞www精品视频免费看| 国产亚洲精AA在线观看SEE| 51在线视频免费观看视频| 亚洲色大网站WWW永久网站| 亚洲男人天堂2020| 13一14周岁毛片免费| 麻豆91免费视频| 亚洲色av性色在线观无码| 国产18禁黄网站免费观看| 国产免费AV片在线观看 | 亚洲成Av人片乱码色午夜| 黄网站色在线视频免费观看| 阿v视频免费在线观看|