对已经生成了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输出流做处理, 拦截HTML文本(asp.net webForm版)的更多相关文章

  1. 拦截asp.net mvc输出流做处理, 拦截HTML文本(asp.net MVC版)

    以前的一个贴子写过一个webForm的拦截HTML输出流的版本,最近用到mvc时用同样的方式发生一些问题. 如下图 查了好久也不知道啥原因. 好吧, 我最后选择放弃. 想起以前自定义Response. ...

  2. 拦截asp.net输出流做处理

    本文标题是指对已经生成了HTML的页面做一些输出到客户端之前的处理. 方法的原理是:把Response的输出重定向到自定义的容器内,也就是我们的StringBuilder对象里,在HTML所有的向页面 ...

  3. 【spring】在spring cloud项目中使用@ControllerAdvice做自定义异常拦截,无效 解决原因

    之前在spring boot服务中使用@ControllerAdvice做自定义异常拦截,完全没有问题!!! GitHub源码地址: 但是现在在spring cloud中使用@ControllerAd ...

  4. vue-router做路由拦截时陷入死循环

    今天分享一下使用vue-router做路由拦截时遇到的坑. 需要提前了解的api 1:router.beforeEach( to , from ,next) ; to: Route: 即将要进入的目标 ...

  5. springboot+springmvc拦截器做登录拦截

    springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration ...

  6. asp.net core 使用中间件拦截请求和返回数据,并对数据进行加密解密。

    原文:asp.net core 使用中间件拦截请求和返回数据,并对数据进行加密解密. GitHub demo https://github.com/zhanglilong23/Asp.NetCore. ...

  7. Struts2拦截器之ExceptionMappingInterceptor(异常映射拦截器)

    一.异常拦截器是什么? 异常拦截器的作用是提供一个机会,可以设置在action执行过程中发生异常的时候映射到一个结果字符串而不是直接中断. 将异常整合到业务逻辑中,比如在分层系统的调用中可以从底层抛出 ...

  8. Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求

    Java过滤器处理Ajax请求,Java拦截器处理Ajax请求,拦截器Ajax请求 >>>>>>>>>>>>>>&g ...

  9. springboot整合拦截器如何让其不拦截默认的访问路径

    1.注册自定义拦截器2.拦截器3.控制器4.其它说明:我想做控制拦截登陆,将所有的请求拦截下来判断如果当前的session里没有用户名则跳转到登陆页面.问题是目前可以拦截所有请求了,但第一次进入登陆页 ...

随机推荐

  1. rzsz的安装

    rz,sz是Linux/Unix同Windows进行ZModem文件传输的命令行工具优点:比ftp命令方便,而且服务器不用打开FTP服务. sz:将选定的文件发送(send)到本地机器rz:运行该命令 ...

  2. 制作jar包

    1.打开cmd 2.通过cd切换到要打包的工程所在的bin目录(一定是bin目录) 运行jar -cvf aa.jar *.* jar是打包的命令 -cvf可以自行查看一下文档解释(jar -help ...

  3. 各个Maven仓库镜像(包括国内)

    各个Maven仓库镜像(包括国内) 衽孤魍墓 ゅ槭 众矿工唯唯诺诺我在旁哭笑不得原 宦蠃サ 骘猩池 粑涫汾滹 吧滔哌蹋 飑俗た 狃攵庾唾 想必是想挡住什么我想反正这笔筒也不是 翡蜮胼 娴左 ...

  4. centos 7 连接 xshell5

    首先 保证你的centos版本与你选择的linux版本相同. 1.首先查看本机IP和网关 2.在centos7命令行下输入nmtui 进入   Edit a commection 选择Edit 按照刚 ...

  5. Java. Warning – Build path specifies execution environment J2SE-1.5

    Build path specifies execution environment J2SE-1.5. There are no JREs installed in the workspace th ...

  6. MySQL数据库传输BLOB类型数据丢失 解决办法

    修改MySQL安装目录下my.ini文件配置:

  7. linux-c/c++调试利器gdb、ddd小试

    linux-c/c++调试利器gdb.ddd小试 原文链接: http://deepfuture.iteye.com/blog/749148 博客分类: C++/C/lisp CC++C#LinuxU ...

  8. @Scheduled(cron = "0 0 * * * ?")实现定时任务

    //每一个小时执行一次 @Scheduled(cron = "0 0 * * * ?") public void saveDailyScoreScheduled() { try { ...

  9. Javaweb 第12天 JSP、EL技术

    第12天 JSP.EL技术 今日任务: JSP技术入门和常用指令 JSP的内置对象&标签介绍 EL表达式&EL的内置对象 课堂笔记 1.JSP技术入门和常用指令 1.1.JSP的由来. ...

  10. RPC学习

    之前有一篇文章,说了RPC的内容: http://www.cnblogs.com/charlesblc/p/6214391.html 如果有一种方式能让我们像调用本地服务一样调用远程服务,而让调用者对 ...