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

攔截asp.net輸出流并進(jìn)行處理的方法

本文實(shí)例主要實(shí)現(xiàn)對已經(jīng)生成了HTML的頁面做一些輸出到客戶端之前的處理。

方法的實(shí)現(xiàn)原理是:把Response的輸出重定向到自定義的容器內(nèi),也就是我們的StringBuilder對象里,在HTML所有的向頁面輸出都變成了向StringBuilder輸出,然后我們對StringBuilder處理完成之后,再把Response的輸出重定向到原來的頁面上,然后再通過Response.Write方法把StringBuilder的內(nèi)容輸出到頁面上。

這里之所以用反射,是因?yàn)镽esponse對象的OutPut屬性是只讀的,通過反編譯該類的程序集發(fā)現(xiàn),OutPut實(shí)際上是內(nèi)部私有成員 _writer來實(shí)現(xiàn)輸出的。因此通過反射來改寫該成員的值以實(shí)現(xiàn)輸出流的重定向。

具體功能代碼如下:

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);//一個(gè)StringWriter,用來獲取頁面內(nèi)容     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來實(shí)現(xiàn):  

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);//一個(gè)StringWriter,用來獲取頁面內(nèi)容     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程序設(shè)計(jì)有一定的借鑒價(jià)值。

AspNet技術(shù)攔截asp.net輸出流并進(jìn)行處理的方法,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 夫外出被公侵犯日本电影 | 美女漏bb | 久久精品亚洲 | 少妇两个奶头喷出奶水了怎么办 | 亚洲成色WWW久久网站夜月 | 伦理片97影视网 | 久久99国产精品自在自在 | 免费观看成人www精品视频在线 | 成激人情在线影院920 | qvod影院| 含羞草影院AE在线观看 | 晚夜免费禁用十大亏亏 | 国产超碰精久久久久久无码AV | 2021国产精品视频 | 最近中文字幕2019免费版日本 | 亚洲人成色777777老人头 | XXX国产麻豆HD | 视频专区亚洲欧美日韩 | 野花韩国中文版免费观看 | 国内高清在线观看视频 | 免费看男人J放进女人J无遮掩 | 精品香蕉99久久久久网站 | 国内外成人免费在线视频 | 两个人的视频hd全免费 | 国语对白刺激真实精品 | 亚洲国产货青视觉盛宴 | 8x华人免费视频 | 亚洲中文热码在线视频 | 午夜福利体验免费体验区 | 伊人影院蕉久 | 亚洲一区免费香蕉在线 | 日本免费一区二区三区最新vr | 无码国产成人午夜在线观看不卡 | 成人网站国产在线视频内射视频 | 色裕插插插影视 | 麻豆影视在线直播观看免费 | 亚洲国产高清福利视频 | 久久热精品18国产 | 小柔的性放荡羞辱日记动漫 | 宿舍BL 纯肉各种PLAY H | 国产精品白浆精子流水合集 |