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(结果 ...
随机推荐
- junit单元测试踩过的坑
1.测试方法不能直接获取到系统初始化的配置信息,需要专门读取 2.单元测试多线程子线程不执行,不会像主线程一样等待子线程退出而退出, 会直接退出. . https://blog.csdn.net/yu ...
- 看图就会-react条件渲染的5种方式
- ImageUtils excel 中 emf 转图片(解决图片上部分显示不全问题)图片转文字
excel 中ActiveX 工具 中的textbox ,以及公式 解析后为emf 图片, emf 转图片(解决图片上部分显示不全问题) 图片转文字 /*********************** ...
- flask - fastapi (python 异步API 框架 可以自动生成swagger 文档) 常用示例 以及整合euraka nacos
flask - fastapi (python 异步API 框架 可以自动生成swagger 文档) 常用示例: 之前使用 flask 需要手动写文档, 这个可以自动生成, fastapi ...
- mysql 5.7启动报错
mysql 5.7 yum 安装完启动报错,如图: 处理步骤:查看/etc/my.cnf 数据存放目录,将里面内容移除到/opt后,启动mysql正常.
- MathJax使用
转载网址: http://t.zoukankan.com/Dean0731-p-12881872.html
- CSS clip-path 属性
属性定义及使用说明 clip-path 属性使用裁剪方式创建元素的可显示区域.区域内的部分显示,区域外的隐藏.可以指定一些特定形状. 注意: clip-path 属性将替换已弃用的 clip 属性. ...
- Java数据脱敏(手机号|邮箱号|身份证号|银行卡号)
参考博客:https://blog.csdn.net/ywb201314/article/details/107762279
- 11.4 显示窗口(harib08d)11.5 小实验(hearib08e) 11.6 高速计数器(harib08f)
11.4 显示窗口(harib08d) 书P206 11.5 小实验(hearib08e) 书P208 11.6 高速计数器(harib08f) 书P209
- 论文翻译:2023_THLNet: two-stage heterogeneous lightweight network for monaural speech enhancement
论文地址:THLNet: 用于单耳语音增强的两级异构轻量级网络 代码:https://github.com/dangf15/THLNet 引用格式:Dang F, Hu Q, Zhang P. THL ...