濮阳杆衣贸易有限公司

主頁 > 知識庫 > 攔截asp.net輸出流并進(jìn)行處理的方法

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

熱門標(biāo)簽:昆明語音電銷機(jī)器人價(jià)格 百度地圖怎樣做地圖標(biāo)注 柳州電銷機(jī)器人公司 浦發(fā)電話機(jī)器人提醒還款 400電話如何申請取消 騰訊地圖標(biāo)注手機(jī) 電銷語音機(jī)器人型號參數(shù) 太原400電話上門辦理 征途美甲店地圖標(biāo)注

本文實(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à)值。

您可能感興趣的文章:
  • 快速插入大量數(shù)據(jù)的asp.net代碼(Sqlserver)
  • asp.net 讀取文本文件并插入數(shù)據(jù)庫的實(shí)現(xiàn)代碼
  • asp.net 使用SqlBulkCopy極速插入數(shù)據(jù)到 SQL Server
  • ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子
  • Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
  • 推薦8項(xiàng)提高 ASP.NET Web API 性能的技術(shù)
  • asp.net利用后臺(tái)實(shí)現(xiàn)直接生成html分頁的方法
  • asp.net頁面SqlCacheDependency緩存實(shí)例
  • Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)

標(biāo)簽:蘭州 天門 新疆 江蘇 德陽 張家界 白山 陽泉

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《攔截asp.net輸出流并進(jìn)行處理的方法》,本文關(guān)鍵詞  攔截,asp.net,輸出,流,并,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《攔截asp.net輸出流并進(jìn)行處理的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于攔截asp.net輸出流并進(jìn)行處理的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    于都县| 青川县| 响水县| 全南县| 阿荣旗| 焦作市| 化德县| 交城县| 芮城县| 湘西| 长葛市| 澄城县| 九江市| 上虞市| 株洲市| 米泉市| 鄯善县| 阳曲县| 双城市| 灌阳县| 雅江县| 梁河县| 东光县| 瓦房店市| 安新县| 上饶县| 肇源县| 永泰县| 体育| 简阳市| 永丰县| 巴林左旗| 高阳县| 武陟县| 刚察县| 泰来县| 宁明县| 辽源市| 会昌县| 易门县| 尼木县|