您的位置:首页 > 其它

一个页面标题和过滤输出的解决方案

2011-04-25 11:23 525 查看
首先要提到一个东西:Response.Filter,这个filter可以让你捕到最后的html输出,之后,就是对输出的html进行文本替换的问题了,当然了,为了能获得每次请求的输出,这里请出了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方法,所有的文本替换都在这Write方法里处理了。

方法代码主要表现为如下:

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 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);
}

看说明就知道分三步走:读取原来的,然后替换,最后写回去,当然了,要注意你的网站编码是UTF8还是GB2312,简单改一下,这里的重点是,我扩展了替换那一块,我用了一个Dictionary,然后循环替换,当然支持正则,所以替换的原始文字和替换后的文字就对应上两个string上了,为了可扩展与方便大伙,我定义了一个抽象类,先实现了三个正则用于截取标题,说明,和关键字,如果还要过滤其它文本.....

上面说到:为了可扩展与方便大伙,我定义了一个抽象类,先实现了三个正则用于截取标题,说明,和关键字。

这里抽象类代码如下:

public abstract class ReplaceTextListBase
{
///
/// 将被返回的替换文本集合列表
///
public Dictionary replaceTextList = new Dictionary();
///
/// 获取当前请求页面的url信息
///
public Uri PageUrl { get { return HttpContext.Current.Request.Url; } }
///
/// 获取html的title的正则
///
public string TitleRegex { get { return ".*"; } }
public string TitleFormat(string titleText)
{
return "";
}
///
/// 获取html的Description的正则
///
public string DescriptionRegex { get { return "]+name=[\"\']description[^<>]*[/]>"; } }
public string DescriptionFormat(string descriptionText)
{
return "";
}
///
/// 获取html的Keyword的正则
///
public string KeywordRegex { get { return "]+name=[\"\']keywords[^<>]*[/]>"; } }
public string KeywordFormat(string keywordText)
{

; return "";
}
///
/// 复写此方法,调用replaceTextList.add()方法后,return replaceTextList;
///
///
public virtual Dictionary GetReplaceTextList()
{
return replaceTextList;
}
}

看完这抽象类后,发现一个虚方法GetReplaceTextList(), 重点就在了

现在看一下我的实例中的子类的实现

public class ReplaceTextList:ReplaceTextListBase
{
public override System.Collections.Generic.Dictionary GetReplaceTextList()
{
replaceTextList.Add(TitleRegex,TitleFormat("TitleRegex"));
replaceTextList.Add(DescriptionRegex,DescriptionFormat("descriptionttest"));
replaceTextList.Add(KeywordRegex,KeywordFormat("keywordadfdfdf"));
return replaceTextList;
}
}

这个例子中的子类实现很简单,就复写了一个虚方法,然后最终页面的输出就是标题为:

TitleRegex,其它两个一看就知了,当然还有如果要替换其它或过滤文件,只要写多几个add方法把要替换的文字给替换掉就行了,替换的文字可以结合数据库.

其实,只是我例子上的简单,直接就定死了标题为TitleRegex

所以,其实在这里才是真正给用户自己扩展的地方

看到我的抽象类里留下了一个PageUrl吧,其实这里就是重点了,如何根据Url查出Title和description和keyword,这就是用户自己的实现的了

当然这里可以给出一些思路:

1.建数据库表,然后对url主机头进行分类管理,自己定义替换字符等

其实就一查询,接下来自己爱怎么弄就怎么弄了。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: