拦截asp.net输出流并进行处理的方法

所属分类: 网络编程 / ASP.NET 阅读数: 743
收藏 0 赞 0 分享

本文实例主要实现对已经生成了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程序设计有一定的借鉴价值。

更多精彩内容其他人还在看

使用.NET Core实现饿了吗拆红包功能

这篇文章主要介绍了使用.NET Core实现饿了吗拆红包功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux服务器下利用Docker部署.net Core项目的全过程

这篇文章主要给大家介绍了关于在Linux服务器下利用Docker部署.net Core项目的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.Net学习笔记之Layui多图片上传功能

这篇文章主要给大家介绍了关于.Net学习笔记之Layui多图片上传功能的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.Net具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET第一次访问慢的完美解决方案(MVC,Web Api)

这篇文章主要给大家介绍了关于ASP.NET第一次访问慢的完美解决方案(MVC,Web Api)的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

aspnetcore 实现简单的伪静态化功能

这篇文章主要介绍了aspnetcore 实现简单的伪静态化功能,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

.NET生成水印更好的方法实例代码

这篇文章主要给大家介绍了关于.NET中生成水印更好的方法,文中通过示例代码介绍的非常详细,对大家学习或者使用.NET具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.NET生成动态验证码的完整步骤

这篇文章主要给大家介绍了关于.NET生成动态验证码的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.NET具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

WCF中使用nettcp协议进行通讯的方法

这篇文章主要给大家介绍了关于WCF中使用nettcp协议进行通讯的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WCF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

WCF如何绑定netTcpBinding寄宿到控制台应用程序详解

这篇文章主要给大家介绍了关于WCF如何绑定netTcpBinding寄宿到控制台应用程序的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WCF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.Net项目中NLog的配置和使用实例详解

这篇文章主要给大家介绍了关于.Net项目中NLog的配置和使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.Net具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多