欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

這里之所以用反射,是因為Response對象的OutPut屬性是只讀的,通過反編譯該類的程序集發(fā)現(xiàn),OutPut實際上是內(nèi)部私有成員 _writer來實現(xiàn)輸出的。因此通過反射來改寫該成員的值以實現(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);//一個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來實現(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);//一個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è)計有一定的借鑒價值。

相關(guān)文章

  • .NET 5 部署在docker上運行的方法

    .NET 5 部署在docker上運行的方法

    這篇文章主要介紹了.NET 5 部署在docker上運行的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • C# OWC生成圖表

    C# OWC生成圖表

    最近做一個項目,按客戶需求,需要生成一些報表,OWC是比較合適的組件.
    2009-06-06
  • asp.net querystring亂碼解決方法

    asp.net querystring亂碼解決方法

    GB2312的網(wǎng)站如果直接用javascript進行ajax數(shù)據(jù)提交,會導(dǎo)致querystring亂碼,下面提供一下解決方法
    2014-02-02
  • 給Repeater控件里添加序號的5種才常見方法介紹

    給Repeater控件里添加序號的5種才常見方法介紹

    Repeater是我們經(jīng)常用的一個顯示數(shù)據(jù)集的數(shù)據(jù)控件那么我們該怎么為Repeater控件添加序號呢?下面編輯為大家介紹幾種常用的為Repeater控件添加序號的方法
    2013-09-09
  • .Net 實現(xiàn)圖片縮略圖上傳通用方法

    .Net 實現(xiàn)圖片縮略圖上傳通用方法

    這篇文章主要介紹了.Net 實現(xiàn)圖片縮略圖上傳通用方法,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • WPF項目在設(shè)計界面調(diào)用后臺代碼

    WPF項目在設(shè)計界面調(diào)用后臺代碼

    這篇文章介紹了WPF項目在設(shè)計界面調(diào)用后臺代碼的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • mvc實現(xiàn)圖片驗證碼功能

    mvc實現(xiàn)圖片驗證碼功能

    這篇文章主要為大家詳細介紹了mvc實現(xiàn)圖片驗證碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • ASP.NET 導(dǎo)出到Excel時保留換行的代碼

    ASP.NET 導(dǎo)出到Excel時保留換行的代碼

    由于Excel畢竟不是 HTML,它有自己的樣式標準,在Excel 中,實現(xiàn)換行的方法是
    2008-12-12
  • IIS中ASP.NET連接SQL Server出錯的解決方法

    IIS中ASP.NET連接SQL Server出錯的解決方法

    在IIS中運行的ASP.NET應(yīng)用程序其所屬用戶名為ASPNET的特定用戶,其默認權(quán)限是無法訪問SQL Server的,更不可能訪問ASP.NET應(yīng)用程序的數(shù)據(jù)庫了,因此要在IIS中訪問SQL Server就需要給ASPNET帳戶賦予相應(yīng)的權(quán)限.
    2010-03-03
  • Entity?Framework?Core生成數(shù)據(jù)庫表

    Entity?Framework?Core生成數(shù)據(jù)庫表

    這篇文章介紹了Entity?Framework?Core生成數(shù)據(jù)庫表的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03

最新評論