拦截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输出流做处理, 拦截HTML文本(asp.net webForm版)
对已经生成了HTML的页面做一些输出到客户端之前的处理 方法的原理是:把Response的输出重定向到自定义的容器内,也就是我们的StringBuilder对象里,在HTML所有的向页面输出都变 成了 ...
- 拦截asp.net输出流并进行处理的方法
本文实例主要实现对已经生成了HTML的页面做一些输出到客户端之前的处理. 方法的实现原理是:把Response的输出重定向到自定义的容器内,也就是我们的StringBuilder对象里,在HTML所有 ...
- 拦截asp.net mvc输出流做处理, 拦截HTML文本(asp.net MVC版)
以前的一个贴子写过一个webForm的拦截HTML输出流的版本,最近用到mvc时用同样的方式发生一些问题. 如下图 查了好久也不知道啥原因. 好吧, 我最后选择放弃. 想起以前自定义Response. ...
- ASP.NET CORE做的网站运行在docker实践
用VS2017 建立了 DotNet Core 2.2 的网站后,如何转移到 Docker 下运行? 下面分两种方式来实践: 1.直接手动命今行,将本机目录映射进Docker,运行网站.2.制作 Im ...
- ASP.NET MVC 做的网站项目
感谢博客园团队日夜为广大需要获取知识人们所做的奉献 博客园团队您们辛苦了 ASP.NET MVC 实现有论坛功能的网站(有iis发布网站 这是之前写的... www.lazyfitness.cn 经过 ...
- 牛腩学ASP.NET CORE做博客(视频)
牛腩学习ASP.NET CORE做的项目,边学边做. 目录: 01-dotnetcore网站部署到centos7系统上(时长 2:03:16) 02-前期准备及项目搭建 (时长:0:23:35) 03 ...
- win10 uwp 使用 asp dotnet core 做图床服务器客户端
原文 win10 uwp 使用 asp dotnet core 做图床服务器客户端 本文告诉大家如何在 UWP 做客户端和 asp dotnet core 做服务器端来做一个图床工具 服务器端 从 ...
- win10 uwp 手把手教你使用 asp dotnet core 做 cs 程序
本文是一个非常简单的博客,让大家知道如何使用 asp dot net core 做后台,使用 UWP 或 WPF 等做前台. 本文因为没有什么业务,也不想做管理系统,所以看到起来是很简单. Visua ...
- ASP.NET输出流至少要有256个字节的数据后Response.Flush方法才会生效
很多时候我们写的asp.net程序会因为做很多操作,所以会花上一分钟甚至几分钟时间.为了使软件使用者能够耐心的等待程序的执行,我们经常会希望有一个进度条来表示程序执行的状态.或者最起码要显示一个类似: ...
随机推荐
- DEBUG_NEW和THIS_FILE
C++ 的一个 比较晦涩难懂的特点是你可以重载 new 操作符,并且你甚至可以给它附加参数.通常,操作符 new 只接受拟分配对象的大小: void* operator new(size ...
- Docker 安装与常用命令介绍
docker的镜像文件作用就是:提供container运行的文件系统层级关系(基于AUFS实现),所依赖的库文件.已经配置文件等等. 安装docker yum install -y docker 启动 ...
- div跟随鼠标移动
1.目标是实现div跟随鼠标而移动,分三种情况进行实现 a)首先获取div,进行绑定鼠标移动事件,给div开启定位功能 第一种实现方式,假如body的大小跟页面大小一样,则可以用这个方法. 1)获取鼠 ...
- 201621123037 《Java程序设计》第10周学习总结
作业10-异常 标签(空格分隔): Java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7 ...
- 【数据库_Postgresql】数据库主键自增长之加序列和不加序列2种方法
将表的主键进行序列增加之后可以在数据库层面自动主键id增长 方法如下:先建序列,然后建表关联id主键,然后添加语句,不用考虑id主键 DROP SEQUENCE IF EXISTS "pub ...
- 用Racket语言写了一个万花筒的程序
用Racket语言写了一个万花筒的程序 来源:https://blog.csdn.net/chinazhangyong/article/details/79362394 https://github. ...
- hive 分区表和分桶表
1.创建分区表 hive> create table weather_list(year int,data int) partitioned by (createtime string,area ...
- Oracle Parameter使用
string sqlStr = "update sys_case t set t.content =:CONTENT,t.property=:PROPERTY where id=:ID&qu ...
- 【CodeChef-SPCLN】Cleaning the Space
https://odzkskevi.qnssl.com/7dfb262544887eff6fb35bfb444759d6?v=1502084197 做法是类似于最大割之类的东西,把每个碎片按照按钮拆点 ...
- 洛谷 U14475 部落冲突 【比赛】 【树链剖分 + 线段树】
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...