天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

攔截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程序設計有一定的借鑒價值。

AspNet技術攔截asp.net輸出流并進行處理的方法,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 色偷偷91综合久久噜噜 | 久久精品国产午夜伦班片 | 久久国产免费观看精品1 | 2019天天射干网站 | 日韩黄色软件 | 青青热久久综合网伊人 | 亚洲 欧美 国产 在线 日韩 | 久草青青在线 | 中文字幕精品在线观看 | 欧美日韩中文国产一区 | 亚洲视频免费在线观看 | 日本激情在线 | 国拍在线精品视频免费观看 | 成人毛片免费观看视频大全 | 成人影院久久久久久影院 | 超污视频带污疼免费视频 | 色欲AV亚洲永久无码精品 | 任你躁国语自产二区在线播放 | 黄色网址在线播放 | 奶头从情趣内衣下露了出来AV | 美女夫妻内射潮视频 | 国产福利视频一区二区 | 久久a级片 | 亚洲精品沙发午睡系列 | 噜噜噜狠狠夜夜躁 | 热久久伊大人香蕉网老师 | 伊人久久大香线蕉综合色啪 | 国产美女久久久久久久久久久 | 亚洲精品美女久久久久99 | 动漫H片在线观看播放免费 动漫H片在线播放免费高清 | 国产成人片视频一区二区青青 | 国产 有码 无码 电影 | 九九热精品在线观看 | 久久99re热在线观看视频 | 亚洲第一色网站 | 无套内射纹身女视频 | 午夜一级毛片看看 | 国产三级在线免费 | 天堂Av亚洲欧美日韩国产综合 | 青草久久精品亚洲综合专区 | 十九岁在线观看免费完整版电影 |