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-bash打开自动执行某条命令的快捷方式创建
"C:\Program Files\Git\git-bash.exe" -c "npm run dev" 创建一个快捷方式,在目标里面加上以上参数,然后运行. ...
- 【Leetcode】 剑指offer:栈与队列 --Day01
写在前面 2023届秋招形势严峻,作为2024届本科生倍感压力.时间紧迫,需要加快脚步. 计划之一是在未来的36天时间里通关Leetcode的剑指offer系列算法题.这一系列的学习周期为31天,也就 ...
- Keil Jlink没法找到STM32H750
https://www.amobbs.com/thread-5713382-1-1.html MDK使用的是5.32,jlink使用的是9.2jlink驱动使用的是6.44b 删除工程下的JLinkS ...
- SDK测试标准
测试分类 具体测试项 测试内容 测试方法 文档测试 接口清单 接口清单是否完整,正确,包含提供给开发者的协议所有字段的定义和解释 人工检查 更新说明 要说明新增,删除的接口定义 Demo示例 显示如何 ...
- windos 环境下载安装seata
参考: https://blog.csdn.net/lianghecai52171314/article/details/127330916
- NTP同步时间
什么是NTPNTP:Network Time Protocol(网络时间协议) ️ NTP 是用于同步网络中计算机时间的协议.它的用途是把计算机的时钟同步到世界协调时UTC. UTC:Universa ...
- Web For Pentester靶场搭建 - XSS
Web For Pentester是集成了一些简单的Web常见漏洞的靶场,其中有常见的XSS 文件上传 SQL注入 文件包含等常见漏洞,类似于DVWA Web For Pentester搭建 Web ...
- 《程序员的自我修养》学习笔记——不一样的hello world【第四弹】
不一样的hello world Linux 的系统调用 通过glibc提供的库函数 glibc 是 Linux 下使用的开源的标准 C 库,它是 GNU 发布的 libc 库,即运行时库.glibc ...
- 开源不易、安全慎行,中国软件如何走向文明?丨RTE 技术环境月报 202205
各位开发者小伙伴: 这里是 2022 年第 5 期的 RTE<技术环境月报>--致力于成为对大家"有用"的 Highlight 看板--每月初通过 RTC 开发者社区( ...
- 在Pycharm上使用远程服务器进行调试
前言 缘起 Mac上没有GPU,需要用到学校服务器进行调试,于是产生了这篇博客.0.0bb 前提 首先确保已经将Pycharm配置好,通过SSH连接到服务器上的开发环境,这一步网络上有许多教 ...