最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2014年08月27日 10:23:29   投稿:shichen2014  
這篇文章主要介紹了攔截asp.net輸出流并進(jìn)行處理的方法,比較有實(shí)用性的一個(gè)技巧,需要的朋友可以參考下

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

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

這里之所以用反射,是因?yàn)镽esponse對(duì)象的OutPut屬性是只讀的,通過(guò)反編譯該類(lèi)的程序集發(fā)現(xiàn),OutPut實(shí)際上是內(nèi)部私有成員 _writer來(lái)實(shí)現(xiàn)輸出的。因此通過(guò)反射來(lái)改寫(xiě)該成員的值以實(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原來(lái)的OutPut 
    tw_new = new StringWriter(content);//一個(gè)StringWriter,用來(lái)獲取頁(yè)面內(nèi)容 
    var type_rp = context.Response.GetType(); 
    //通過(guò)反射獲取對(duì)象的私有字段 
    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來(lái)實(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 的摘要說(shuō)明 
/// </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原來(lái)的OutPut 
    tw_new = new StringWriter(_content);//一個(gè)StringWriter,用來(lái)獲取頁(yè)面內(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(); 
  } 
}

相信本文所述對(duì)大家的asp.net程序設(shè)計(jì)有一定的借鑒價(jià)值。

相關(guān)文章

最新評(píng)論

竹北市| 云梦县| 蚌埠市| 大荔县| 天镇县| 永定县| 海南省| 南江县| 巴林右旗| 太康县| 阿坝县| 德令哈市| 锡林浩特市| 文水县| 开平市| 饶河县| 通海县| 泾阳县| 肥乡县| 通城县| 察哈| 高安市| 茌平县| 诏安县| 沙田区| 利川市| 友谊县| 荣昌县| 红河县| 东方市| 克拉玛依市| 南丰县| 武功县| 新竹县| 瓮安县| 革吉县| 旺苍县| 伊金霍洛旗| 沾化县| 辽源市| 渑池县|