攔截asp.net mvc輸出資料流做處理, 攔截HTML文本(asp.net MVC版)

來源:互聯網
上載者:User

標籤:

  以前的一個貼子寫過一個webForm的攔截HTML輸出資料流的版本,最近用到mvc時用同樣的方式發生一些問題。

  如

查了好久也不知道啥原因。

好吧, 我最後選擇放棄。

  想起以前自訂Response.Filter 時,裡面Write方法可以擷取頁面流的資訊。

  這次我借用HttpModule實現攔截HTML內容輸出資料流,下面看代碼

一、HtmlHttpModule.cs    定義一個新類繼承HttpModule

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using System.Web;namespace Monitor{    public class HtmlHttpModule : IHttpModule    {        private HttpApplication _contextApplication;        private TextWriter tw_old;    // 舊流        private StringBuilder _content;        private FieldInfo tw_field;        public void Init(HttpApplication application)        {            _contextApplication = application;            _contextApplication.BeginRequest += new EventHandler(_contextApplication_BeginRequest);            _contextApplication.EndRequest += new EventHandler(_contextApplication_EndRequest);        }        void _contextApplication_BeginRequest(object sender, EventArgs e)        {            #region             try            {                _content = new StringBuilder();                _contextApplication.Response.Filter = new DefaultFilter(_contextApplication.Response.Filter, o => _content.Append(o));   //用自訂的Filter篩選器            }            catch (Exception ex)            {                //這裡寫入日誌            }            #endregion        }        void _contextApplication_EndRequest(object sender, EventArgs e)        {            #region             try            {
          #region
          //這裡就是寫處理HTML的代碼了。現在HTML的文本儲存在_content中,取出處理就可以
          //處理完放出下面的Write中
          #endregion          
_contextApplication.Response.Write(_content.ToString()); } catch (Exception ex) { //這裡寫入日誌 } #endregion } public void Dispose() { _contextApplication = null; if (_contextApplication != null) { _contextApplication.Dispose(); } } }}

 

二、DefaultFilter.cs     在module中我們給Response.Filter 自訂的篩選器

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Text.RegularExpressions;using System.Web;namespace Monitor{    public class DefaultFilter : Stream    {        Stream responseStream;        long position;        Action<String> action;        public DefaultFilter(Stream inputStream,Action<String> act)        {            action = act;            responseStream = inputStream;        }        public override bool CanRead        {            get            {                return responseStream.CanRead;            }        }        public override bool CanSeek        {            get            {                return responseStream.CanSeek;            }        }        public override bool CanWrite        {            get            {                return responseStream.CanWrite;            }        }        public override long Length        {            get            {                return responseStream.Length;            }        }        public override long Position        {            get            {                return position;            }            set            {                position = value;            }        }        public override void Flush()        {            responseStream.Flush();        }        public override int Read(byte[] buffer, int offset, int count)        {            return responseStream.Read(buffer, offset, count);        }        public override long Seek(long offset, SeekOrigin origin)        {            return responseStream.Seek(offset, origin);        }        public override void SetLength(long value)        {            responseStream.SetLength(value);        }        public override void Write(byte[] buffer, int offset, int count)        {            action(HttpContext.Current.Response.ContentEncoding.GetString(buffer, offset, count));        }    }}

 

 

 三、web.config   該建立都建立好了,現在就讓它在mvc中起作用吧

<system.webServer>    <modules>      <add name="Monotpr" type="Monitor.HtmlHttpModule,Monitor"/>    </modules>  </system.webServer>

 

攔截asp.net mvc輸出資料流做處理, 攔截HTML文本(asp.net MVC版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.