婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

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

本文實例主要實現對已經生成了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程序設計有一定的借鑒價值。

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

標簽:蘭州 天門 新疆 江蘇 德陽 張家界 白山 陽泉

巨人網絡通訊聲明:本文標題《攔截asp.net輸出流并進行處理的方法》,本文關鍵詞  攔截,asp.net,輸出,流,并,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《攔截asp.net輸出流并進行處理的方法》相關的同類信息!
  • 本頁收集關于攔截asp.net輸出流并進行處理的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 保康县| 土默特左旗| 祁东县| 安塞县| 宁明县| 永川市| 繁峙县| 肇州县| 罗定市| 上饶市| 沂源县| 汉源县| 吕梁市| 彭山县| 梓潼县| 巧家县| 黄梅县| 重庆市| 娄底市| 绵阳市| 鞍山市| 永善县| 柘荣县| 贡山| 梅河口市| 四子王旗| 古交市| 府谷县| 庆元县| 渝北区| 柳林县| 新竹市| 赫章县| 平远县| 延川县| 荣成市| 和林格尔县| 明星| 方正县| 玉环县| 玉林市|