拦截asp.net输出流并进行处理的方法
本文实例主要实现对已经生成了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购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
- 这篇文章主要介绍了Windows批量搜索并复制/剪切文件的批处理程序实例,需要的朋友可以参考下...2020-06-30
BAT批处理判断服务是否正常运行的方法(批处理命令综合应用)
批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中。这篇文章主要介绍了BAT批处理判断服务是否正常运行(批处理命令综合应用),需要的朋友可以参考下...2020-06-30- file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
- 这篇文章主要介绍了解决Pycharm 运行后没有输出的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-06
- 这篇文章主要介绍了C#多线程中的异常处理操作,涉及C#多线程及异常的捕获、处理等相关操作技巧,需要的朋友可以参考下...2020-06-25
ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- 这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
- 这篇文章主要介绍了postgresql 中的时间处理小技巧(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 这篇文章主要介绍了Python同时处理多个异常的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-29
C#异常处理中try和catch语句及finally语句的用法示例
这篇文章主要介绍了C#异常处理中try和catch语句及finally语句的用法示例,finally语句的使用涉及到了C#的垃圾回收特性,需要的朋友可以参考下...2020-06-25- 这篇文章主要介绍了cmd命令的重定向输出 2>&1详解,需要的朋友可以参考下...2020-08-28
- 这篇文章主要介绍了python如何用moviepy对视频进行简单的处理,帮助大家更好的利用python处理视频,感兴趣的朋友可以了解下...2021-03-11
详解ASP.NET Core 中基于工厂的中间件激活的实现方法
这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22- 这篇文章介绍了C#异常处理,有需要的朋友可以参考一下...2020-06-25
asp.net通过消息队列处理高并发请求(以抢小米手机为例)
这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表
在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19ASP.NET单选按钮控件RadioButton常用属性和方法介绍
RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22- 这篇文章主要介绍了Python如何输出警告信息,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-30