麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

攔截asp.net輸出流并進行處理的方法

2024-07-10 13:28:04
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了攔截asp.net輸出流并進行處理的方法,比較有實用性的一個技巧,需要的朋友可以參考下
 
 

本文實例主要實現對已經生成了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());   } }  

方法三:

public class HttpModule : IHttpModule {   private HttpApplication _contextApplication;   private TextWriter tw_new, 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)   {     _content = new StringBuilder();     tw_old = _contextApplication.Response.Output;     tw_new = new StringWriter(_content);     var type_rp = _contextApplication.Response.GetType();     tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);     tw_field.SetValue(_contextApplication.Response, tw_new);   }   void _contextApplication_EndRequest(object sender, EventArgs e)   {     tw_field.SetValue(_contextApplication.Response, tw_old);     //做自己的處理     _content.AppendLine("<!--jhxz-->");     _contextApplication.Response.Write(_content.ToString());   }   public void Dispose()   {     _contextApplication = null;     _contextApplication.Dispose();   } }

相信本文所述對大家的asp.net程序設計有一定的借鑒價值


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产91av视频 | 欧美精品一级 | 鲁丝一区二区二区四区 | 超级av在线 | 黄色片在线免费播放 | 男女羞羞在线观看 | a免费毛片| 欧美精品一区二区视频 | 亚洲第一色片 | 俄罗斯hdxxx| 日韩视频一 | 亚洲一区在线免费视频 | 九九热精品视频在线播放 | 香蕉秀 | 欧美极品欧美精品欧美视频 | 黄网站色成年大片免费高 | 在线播放黄色片 | 久久久久久久久久久久久久av | 毛片在线免费播放 | 亚洲精品成人久久久 | 免费在线观看亚洲 | 中文字幕国 | 欧美精品18 | 精品久久久久久综合日本 | 欧美性受ⅹ╳╳╳黑人a性爽 | 北原夏美av| 亚洲三区精品 | 久久久久久久国产视频 | 精品一区二区在线观看视频 | 水卜樱一区二区av | 国产精品视频免费在线观看 | 成人三级电影网 | 久久激情小视频 | 国产高潮失禁喷水爽到抽搐视频 | 国产亚洲精品综合一区91555 | 欧美一级美国一级 | 有色视频在线观看 | 久久久久国产成人免费精品免费 | 欧美日韩高清一区 | 亚洲成人在线免费 | 欧美性久久久 |