攔截asp.net輸出資料流並進行處理的方法

來源:互聯網
上載者:User

標籤:index   實現   div   lte   pen   屬性   .net   isp   efault   

本文執行個體主要實現對已經產生了HTML的頁面做一些輸出到用戶端之前的處理。

方法的實現原理是:把Response的輸出重新導向到自訂的容器內,也就是我們的StringBuilder對象裡,在HTML所有的向頁面輸出都變成了向StringBuilder輸出,然後我們對StringBuilder處理完成之後,再把Response的輸出重新導向到原來的頁面上,然後再通過Response.Write方法把StringBuilder的內容輸出到頁面上。

這裡之所以用反射,是因為Response對象的OutPut屬性是唯讀,通過反編譯該類的程式集發現,OutPut實際上是內部私人成員 _writer來實現輸出的。因此通過反射來改寫該成員的值以實現輸出資料流的重新導向。

具體功能代碼如下:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; using System.IO; using System.Reflection; public partial class _Default : System.Web.UI.Page  {   StringBuilder content = new StringBuilder();   TextWriter tw_old, tw_new;   FieldInfo tw_field;   protected void Page_Load(object sender, EventArgs e)   {     var context = HttpContext.Current;       tw_old = context.Response.Output;//Response原來的OutPut     tw_new = new StringWriter(content);//一個StringWriter,用來擷取頁面內容     var type_rp = context.Response.GetType();     //通過反射擷取對象的私人欄位     tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);     tw_field.SetValue(context.Response, tw_new);   }   protected override void Render(HtmlTextWriter writer)   {     base.Render(writer);     //替換回Response的OutPut     tw_field.SetValue(HttpContext.Current.Response, tw_old);     //做自己的處理     content.AppendLine("<!--江湖小子-->");     HttpContext.Current.Response.Write(content.ToString());   } } 方法二,用HttpModul來實現:  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.IO; using System.Text; using System.Reflection; /// <summary> ///HttpModule 的摘要說明 /// </summary> public class HttpModule : IHttpModule {   private HttpApplication _contextApplication;   private TextWriter tw_new, tw_old;   private StringBuilder _content;   private FieldInfo tw_field;   public void Init(HttpApplication context)   {     _contextApplication = context;     _contextApplication.PreRequestHandlerExecute += new EventHandler(_contextApplication_PreRequestHandlerExecute);   }   public void Dispose()   {     _contextApplication = null;     _contextApplication.Dispose();   }   public void _contextApplication_PreRequestHandlerExecute(object sender, EventArgs e)   {     HttpContext context = _contextApplication.Context;       var _page = context.Handler as System.Web.UI.Page;     _page.Unload += new EventHandler(_page_Unload);       _content = new StringBuilder();     tw_old = context.Response.Output;//Response原來的OutPut     tw_new = new StringWriter(_content);//一個StringWriter,用來擷取頁面內容     var type_rp = context.Response.GetType();     tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);     tw_field.SetValue(context.Response, tw_new);   }   void _page_Unload(object sender, EventArgs e)   {     //替換回Response的OutPut     tw_field.SetValue(HttpContext.Current.Response, tw_old);     //做自己的處理     _content.AppendLine("<!--江湖小子-->");     HttpContext.Current.Response.Write(_content.ToString());   } }

攔截asp.net輸出資料流並進行處理的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.