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(结果 ...
随机推荐
- 解决idea翻译失败问题
修改host(windows)(2022-11-09) 进入该目录,C:\Windows\System32\drivers\etc,hosts文件上右键,把hosts文件的只读去了 打开hosts文件 ...
- SQL 抽象语法树及改写场景应用
SQL 抽象语法树及改写场景应用 1 背景 我们平时会写各种各样或简单或复杂的 sql 语句,提交后就会得到我们想要的结果集.比如 sql 语句,"select * from t_user ...
- 在app中如何使weib-view不铺满全屏,自适应页面
// #ifdef APP-PLUS //自建webview var currentWebview = this.$scope.$getAppWebview(); var height = this. ...
- Python中的startswith()函数用法
函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一.函数说明语法:string.startswith(str, beg=0,end=len(string)) ...
- @Value属性值读取
1.在父类定义属性DQ,并通过配置初始化 @Configuration public class DQConfig { public static String DQ; @Value("${ ...
- 模拟多路开关mux的数据耦合问题
1.前言 最近在做有关sensor的项目时遇到了一个关于多路选择器引起的数据耦合问题,具体的问题现象和解决方案如下: 2.多路开关的介绍 2.1 概述 多路开关:在多路被测试的信号公用一路A/D转换器 ...
- java8利用流和lambda表达式对list遍历处理
java8的lambda表达式提供了一些方便list操作的方法,主要涵盖分组.过滤.求和.最值.排序.去重. 优点: (1) 简洁,跟之前的传统写法对比,能少写不少代码; (2) 易并行计算.尤其适用 ...
- ansible批量采集、批量互信、批量复制、分发文件
一.先说一下用ansible批量采集机器信息的实现办法: 1.先把要采集的机器信息的IP添加到主节点机器的/etc/ansible/hosts里面: 2.在/etc/ansible/hosts里面添加 ...
- aspnetcore微服务中使用发件箱模式实例
aspnetcore微服务种服务之间的通信一般都有用到消息中间件,如何确保该服务的持久层保存创建的数据同时又把消息成功投递到了关联服务,关联服务做对应的处理. 下面就以一个简单的例子来演示实现方式之一 ...
- spark中的持久化机制以及lineage和checkpoint(简含源码解析)
spark相比MapReduce最大的优势是,spark是基于内存的计算模型,有的spark应用比较复杂,如果中间出错了,那么只能根据lineage从头开始计算,所以为了避免这种情况,spark提供了 ...