对已经生成了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. 4天html总结

  2. vue的测试(Vue.js devtool)

    1. 安装chrome插件:Vue.js devtools(https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejigli ...

  3. 关于json和字符串之间的转换

    在最近的工作中,使用到JSON进行数据的传递,特别是从前端传递到后台,前台可以直接采用ajax的data函数,按json格式传递,后台Request即可,但有的时候,需要传递多个参数,后台使用requ ...

  4. maven 运行tomcatrun -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.

      解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment varia ...

  5. angular localStorage使用方法

    var YourCtrl = function($scope, localStorageService, ...) { // To add to local storage localStorageS ...

  6. Arpa's loud Owf and Mehrdad's evil plan

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  7. appium元素定位及操作

    1.测试用例准备 数据准备   前提条件   操作步骤    预期结果 2.TestNG 用例组织:@Test @Before Class 结果验证:Assert 数据驱动:@DataProvide ...

  8. Webform动态创建删除行及后台取值

    开发过程中经常碰到许多不确定事项,所以有时需要动态生成新的记录,如图所示,点击新增时新增一条参考记录,点击删除时则删除该记录:第一步,创建一个表格,用hidden记录当前最大行数,添加时则只需复制模板 ...

  9. 运用bootstrap框架的时候 引入文件的问题

    还要下个jquery,因为bootstrap的js是用jquery写的如果在同一个目录下<html><head><link href="css/bootstra ...

  10. 如何使用Java、Servlet创建二维码

    归功于智能手机,QR码逐渐成为主流,它们正变得越来越有用.从候车亭.产品包装.家装卖场.汽车到很多网站,都在自己的网页集成QR码,让人们快速找到它们.随着智能手机的用户量日益增长,二维码的使用正在呈指 ...