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(结果 ...
随机推荐
- centos7 启动Tomcat7时报错:The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production envi ...
- Fastboot_Cmd
/* -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- */adb命令:/* -*-* ...
- Linux挂载SMB共享文件夹
mount -t cifs -o username=xxxx,password=xxxx //PATH/TO/Shared/Folder /mount/point
- 《MySQL是怎样运行的》第八章小结
- 20个值得收藏的实用JavaScript技巧
1.确定对象的数据类型 function myType(type) { return Object.prototype.toString.call(type).slice(8, -1); 使用Obje ...
- better-scroll横向滚动、纵向滚动
<div ref="tab" class="tab"> <ul ref="tabWrapper" class=" ...
- 深入理解 python 虚拟机:pyc 文件结构
深入理解 python 虚拟机:pyc 文件结构 在本篇文章当中主要给大家介绍一下 .py 文件在被编译之后对应的 pyc 文件结构,pyc 文件当中的一个核心内容就是 python 字节码. pyc ...
- vue中新的状态管理器-pinia
背景 对于pinia的使用,可参考官方文档在这不做过多赘述.这边主要来讲讲pinia中 少用且好用的方法,为什么我们选择pinia而不用vuex ps: 以下写法全部基于组合式API 使用方式: 先下 ...
- Rancher 系列文章-Rancher 升级
概述 之前在 天翼云上用 4 台机器安装了一个 1 master(及 etcd) 3 node 的 K3S 集群,并在其上使用 Helm 安装了 Rancher 2.6.3 版本. 前几天发现 Ran ...
- Unity3D中的Attribute详解(四)
本篇我们将逐一讲解Unity中经常使用的Attribute(Unity对应的文档版本为2018.1b). 首先是Serializable,SerializeField以及NonSerialized,H ...