ASP.NET Response.Filter
寫 ASP.NET 有時候會想要在畫面輸出前一刻進行攔截,並換掉 html 中的特定字元。例如網站中有許多頁面都有 www.google.com.tw 的超連結,我希望在測試機上可以把連結換成 www.microsoft.com.tw ,但又不希望去動到 aspx。這個時候就可以利用 Response.Filter 來做這個事情。
Response.Filter 本身就是一個 Stream 物件,所以要做的事情很簡單,就是再用一個 Stream 把它包起來,然後在 Write 方法加工就行了。為求使用方便,可以再加上 HttpModule 來處理所有 text/html 的回應。
public class CatchText : IHttpModule {
void IHttpModule.Dispose() { }
void IHttpModule.Init(HttpApplication context) {
//註冊事件,在 BeginRequest 的時候把 Response.Filter 換掉
context.BeginRequest += (sender, e) => {
context.Response.Filter =
new CatchTextStream(context.Response.Filter);
};
}
}
public class CatchTextStream : Stream {
private Stream output;
public CatchTextStream(Stream s) {
output = s;
}
public override bool CanRead {
get { return output.CanRead; }
}
public override bool CanSeek {
get { return output.CanSeek; }
}
public override bool CanWrite {
get { return output.CanWrite; }
}
public override void Flush() {
output.Flush();
}
public override long Length {
get { return output.Length; }
}
public override long Position {
get { return output.Position; }
set { output.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count) {
return output.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin) {
return output.Seek(offset, origin);
}
public override void SetLength(long value) {
output.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count) {
StringComparison ignore = StringComparison.CurrentCultureIgnoreCase;
if (HttpContext.Current != null){
HttpContext context = HttpContext.Current;
if (context.Response.ContentType.Equals("text/html", ignore)) {
Encoding encoding = context.Response.ContentEncoding;
//在這邊把 google 換成 microsoft
string html = encoding.GetString(buffer, offset, count)
.Replace("google", "microsoft");
byte[] bytes = encoding.GetBytes(html);
output.Write(bytes, 0, bytes.Length);
} else
output.Write(buffer, offset, count);
}
}
}
整個程式就只有這樣,主要就是在 Write 方法裡面動點手腳而已,剩下的就是設定 web.config,把這個 HttpModule 掛上去。
<httpModules>
<add name="CatchText1" type="CatchText"/>
</httpModules>
接著就可以看到效果了
ASP.NET Response.Filter的更多相关文章
- 收藏一篇关于Asp.net Response.Filter的文章
Capturing and Transforming ASP.NET Output with Response.Filter https://weblog.west-wind.com/posts/20 ...
- asp.net MVC 3/4 equivalent to a response.filter
am in a need to intercept all of the html that will be sent to the browser and replace some tags tha ...
- asp.net 利用Response.Filter 获取输出内容, 变更输出内容
重写 Response.Filter 就可以获取或更新输出到浏览器的内容 资料: https://weblog.west-wind.com/posts/2009/Nov/13/Captur ...
- ASP.NET MVC5 Filter重定向问题
ASP.NET MVC5 Filter重定向问题 一.问题描述 1.在Filter中使用直接filterContext.RequestContext.HttpContext.Response.Redi ...
- ASP.NET Core Filter如何支持依赖注入
通过Filter来支持:分别有IResourceFilter AuthorizeFilter ActionFilter ExceptionFilter ResultFilter,Filter也被称为拦 ...
- 某墙尼妹,用个Response.Filter来解决StackExchange.Exceptional中google cdn的问题
某墙墙了古古路,一些开源的东东里用了古古路CDN,比如Exceptional,Opserver ,导致服务要么慢要么用不了 必须要替换之 Exceptional就只要用Response.Filter替 ...
- Asp.Net Core Filter 深入浅出的那些事-AOP
一.前言 在分享ASP.NET Core Filter 使用之前,先来谈谈AOP,什么是AOP 呢? AOP全称Aspect Oriented Programming意为面向切面编程,也叫做面向方法编 ...
- Asp.Net MVC Filter 实现方式和作用范围控制
MVC中的Filte 简单又优雅的实现了AOP ,在日志,权限,缓存和异常处理等方面用的比较多.但本文不是讨论Filter这些功能点,而是总结Filter实现的方式.说实现也不太准确,也就是它的呈现方 ...
- 学习之-ASP.NET MVC Filter
MVC Filter 是典型的AOP应用,对MVC框架处理客户端请求注入额外的一些逻辑,如日志记录.缓存处理.异常处理和权限验证,性能检测(横切关注点),而这些逻辑通常与主要业务无关,被独立分开作为公 ...
- ASP.Net MVC Filter验证用户登录
一.Filter是什么 ASP.NetMVC模式自带的过滤器Filter,是一种声明式编程方式,支持四种过滤器类型,各自是:Authorization(授权),Action(行为),Result(结果 ...
随机推荐
- 配置git到码云
一.安装完git之后,对项目文件点击右键选择Git Base Here #Git 全局设置用户名与邮箱 git config --global user.name "" git c ...
- Istio思考往前一小步~系列一
思考起源于现实应用需求,随着微服务理念普及,基础设施从单机到容器到Kubernetes,体验过集群的各种好处之后,我们还缺少什么?为什么还要在kubernetes的基础上部署Istio?个人认为Ist ...
- beta冲刺:总结随笔
这个作业属于哪个课程 <班级的链接> 这个作业要求在哪里 <作业要求的链接> 这个作业的目标 beta冲刺总结 作业正文 .... 其他参考文献 ... 一.预期计划 | 6. ...
- Less1-union select 联合查询注入
在学习之前,我们要知道,什么是 SQL 注入? 一句话来说,攻击者通过注入恶意的SQL命令,破坏SQL查询语句的结构,从而达到执行恶意SQL语句的目的. SQL注入漏洞的危害是很大的,常常会导致整个数 ...
- 对于如何在IDEA中给Terminal添加git的详解
具体步骤 1.配置本机环境变量 进入到环境变量的设置界面,然后找到下面的Path变量,双击点开: 然后新建一个变量,路径定义到git的目录下面的bin目录下: 2.WIN+R,然后输入cmd,进入终端 ...
- IDEA学生认证的步骤详解
步骤详解 在上次使用学生认证的方法对jetbrains认证成功之后,咱们在IDEA这里认证一下吧! 一.点击help这里的register 如图所示: 进入这样一个界面: 然后点击左下角的的Log I ...
- JavaWeb学习笔记第三弹
一.数据库设计 1.软件研发步骤 2.数据库设计概念 建立数据库中的表结构以及表与表之间的关联关系的过程 3.数据库设计的步骤 表关系:一对一.一对多(多对一).多对多 表关系之一对多 表关系之多对多 ...
- 第二章 数据和C
2.1错误和警告 如果输入这个程序的过程中出现错误(error),比如少了一个分号,编译器会给出语法错误消息.即使输入正确,编译器还可能发出这样的警告(warning):"警告------从 ...
- flutter widget---->FloatingActionButton
在Flutter中说起Button,floatingActionButton用的也非常的多.今天我们就来学习一下. Simple Example import 'package:flutter/mat ...
- NodeJS V8引擎的内存和垃圾回收器(GC)
一.为什么需要GC 程序应用运行需要使用内存,其中内存的两个分区是我们常常会讨论的概念:栈区和堆区. 栈区是线性的队列,随着函数运行结束自动释放的,而堆区是自由的动态内存空间.堆内存是手动分配释放或者 ...