一个页面标题和过滤输出的解决方案(上)
2023-09-14 08:59:37 时间
filter可以让你截取到最后的html输出,如果你的程序需要在输出之前,做一些处理,用这个比较方便。
{
HttpContext.Current.Response.Filter = new HttpResponseFilter(HttpContext.Current.Response.Filter,new ReplaceTextList());
哪来的:由于Response.Filter 是一个Stream类,所以新类HttpResponseFilter需要继承自Stream,然后复写Write方法,实现自定义方法即可。
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public override void Write(byte[] buffer, int offset, int count)
{
//读出写的文字
byte[] data = new byte[count];
Buffer.BlockCopy(buffer, offset, data, 0, count);
string inputText = Encoding.UTF8.GetString(data);
//开始替换
if (replaceTextList != null replaceTextList.Count 0)
{
foreach (KeyValuePair string, string values in replaceTextList)
{
inputText = Regex.Replace(inputText, values.Key, values.Value, RegexOptions.Singleline);
}
replaceTextList.Clear();
}
replaceTextList = null;
//将替换后的写入response
byte[] newdata = Encoding.UTF8.GetBytes(inputText);
filterStream.Write(newdata, 0, newdata.Length);
}
分三步走:
1:读取原文本内容
2:然后替换修改成自己的内容
3:写回去输出
注意事项:要注意网站编码是UTF8还是GB2312
重点是:我扩展了替换那一块,我用了一个Dictionary string, string
然后循环替换,当然支持正则,所以替换的原始文字和替换后的文字就对应上两个string上了
具体扩展应用见下篇文章。
条码标签打印软件如何实现标签重复打印 重复打印是批量打印中的一种重要的打印方式。在批量打印条码、标签、图片的时候经常会遇到,把同一个标签重复打印多份,或者是把同一张标签纸重复打印多张。那么在条码标签打印软件中如何实现重复打印的呢?下面介绍两种重复打印的方式。
第二个问题,在哪里,如何使用Response.Filter
这里使用上全局的Global.asax处理,在Global.asax的Application_BeginRequest事件里截取html
事件代码复杂?其实就一行:
protected void Application_BeginRequest(object sender, EventArgs e){
HttpContext.Current.Response.Filter = new HttpResponseFilter(HttpContext.Current.Response.Filter,new ReplaceTextList());
代码中的HttpResponseFilter类是什么?
说功能:这类主要实现的功能是,接替默认的Filter,然后换成自定义的Filter,方便处理自己要处理的事情。哪来的:由于Response.Filter 是一个Stream类,所以新类HttpResponseFilter需要继承自Stream,然后复写Write方法,实现自定义方法即可。
复写代码示例:
![复制代码](http://common.cnblogs.com/images/copycode.gif)
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
{
//读出写的文字
byte[] data = new byte[count];
Buffer.BlockCopy(buffer, offset, data, 0, count);
string inputText = Encoding.UTF8.GetString(data);
//开始替换
if (replaceTextList != null replaceTextList.Count 0)
{
foreach (KeyValuePair string, string values in replaceTextList)
{
inputText = Regex.Replace(inputText, values.Key, values.Value, RegexOptions.Singleline);
}
replaceTextList.Clear();
}
replaceTextList = null;
//将替换后的写入response
byte[] newdata = Encoding.UTF8.GetBytes(inputText);
filterStream.Write(newdata, 0, newdata.Length);
}
![复制代码](http://common.cnblogs.com/images/copycode.gif)
代码解读:
![复制代码](http://common.cnblogs.com/images/copycode.gif)
1:读取原文本内容
2:然后替换修改成自己的内容
3:写回去输出
注意事项:要注意网站编码是UTF8还是GB2312
重点是:我扩展了替换那一块,我用了一个Dictionary string, string
然后循环替换,当然支持正则,所以替换的原始文字和替换后的文字就对应上两个string上了
具体扩展应用见下篇文章。
![复制代码](http://common.cnblogs.com/images/copycode.gif)
扩展的小说明:
为了可扩展与方便大伙,我定义了一个抽象类,先实现了三个正则用于截取标题,说明,和关键字,具体应用还是见下文。
条码标签打印软件如何实现标签重复打印 重复打印是批量打印中的一种重要的打印方式。在批量打印条码、标签、图片的时候经常会遇到,把同一个标签重复打印多份,或者是把同一张标签纸重复打印多张。那么在条码标签打印软件中如何实现重复打印的呢?下面介绍两种重复打印的方式。
相关文章
- Flask 学习-36.Flask-RESTful 序列化输出对象
- 2022-07-22:以下go语言代码输出什么?A:1;B:1.5;C:编译错误;D:1.49。package mainimpo
- 2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。pa
- javascript获取当前时间,按指定格式输出_js获取现在本地时间的代码
- PHP通过CURL的方式读取图片接口输出乱码
- 详解linux下nohup日志输出过大问题解决方案
- 快速掌握Linux输出文件内容的技巧(linux输出文件内容)
- Linux命令终端输出重定向(linux命令输出重定向)
- Fintech公司上市故事续集:不只是加码科研、输出技术
- ThinkPHP模版引擎之变量输出详解