標籤:
以前的一個貼子寫過一個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版)