对已经生成了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. STM32F103的11个定时器详解(转)

    源:STM32F103的11个定时器详解 STM32F103系列的单片机一共有11个定时器,其中:2个高级定时器4个普通定时器2个基本定时器2个看门狗定时器1个系统嘀嗒定时器 出去看门狗定时器和系统滴 ...

  2. spring+ibatis问题1—— 程序报错:java.sql.SQLException: Io 异常: Connection reset by peer, socket write error; ”或“java.sql.SQLException 关闭的连接”异常

    转自:http://blog.sina.com.cn/s/blog_1549fb0710102whz2.html spring+ibatis程序测试时报错:java.sql.SQLException: ...

  3. 特殊函数(__all__)

    python里__all__ 属性分别于模块和包之中的用法 一. 在模块(*.py)中使用意为导出__all__列表里的类.函数.变量等成员,否则将导出modualA中所有不以下划线开头(私有)的成员 ...

  4. 用两个栈实现队列,剑指offer P59

    public class QueueByStack { private Stack<Integer> stack1; private Stack<Integer> stack2 ...

  5. 旋转数组中的最小数字,剑指offer,P70 二分查找来实现O(logn)的查找

    public class MinNumberInRotatedArray { public int getMinNumInRotatedArray(int[] array) { if(array == ...

  6. Java继承多态中的方法访问权限控制

    java中的方法天生具有继承多态特性,这点与C++有很大不同(需要在父类方发上加virtual关键字),但用起来确实方便了许多. 最简单的继承多态 声明一个接口BaseIF,只包含一个方法声明 pub ...

  7. 【HELLO WAKA】WAKA iOS客户端 之一 APP分析篇

    由于后续篇幅比较大,所以调整了内容结构. 全系列 [HELLO WAKA]WAKA iOS客户端 之一 APP分析篇 [HELLO WAKA]WAKA iOS客户端 之二 架构设计与实现篇 [HELL ...

  8. Python os.path模板函数

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  9. 工艺成型及仿真、铸造工艺及仿真ProCAST软件入门认识介绍

    视频源:技术邻 关键词:ProCAST.工艺成型及仿真.铸造工艺及仿真 简介:ProCAST 软件是由美国 USE 公司开发的铸造过程的模拟软件采用基于有限元(FEM)的数值计算和综合求解的方法,对铸 ...

  10. 网站生产app的一些网址

    1.http://www.staticgen.com/2.http://siteapp.baidu.com3.http://www.apicloud.com