public class SetToken :System.Web.UI.Page
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
base.Render(htmlWriter);
string html = sw.ToString() ;
Regex obj = new Regex("<a.*?(?: |\\\\t|\\\\r|\\\\n)?href=[\\'\"]?(.+?)[\\'\"]?(?:(?: |\\\\t|\\\\r|\\\\n)+.*?)?>(.+?)<\\/a.*?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = obj.Matches(html);
CompareList list = new CompareList();
foreach (Match match in matches)
{
string url = match.Groups[1].Value.ToLower();
list.Add(url);
}
foreach (string url in list)
{
string newUrl=Utils.WriteUrl(url);
html = html.Replace(url, newUrl);
}
writer.Write(html) ;
}
}
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
base.Render(htmlWriter);
string html = sw.ToString() ;
Regex obj = new Regex("<a.*?(?: |\\\\t|\\\\r|\\\\n)?href=[\\'\"]?(.+?)[\\'\"]?(?:(?: |\\\\t|\\\\r|\\\\n)+.*?)?>(.+?)<\\/a.*?>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = obj.Matches(html);
CompareList list = new CompareList();
foreach (Match match in matches)
{
string url = match.Groups[1].Value.ToLower();
list.Add(url);
}
foreach (string url in list)
{
string newUrl=Utils.WriteUrl(url);
html = html.Replace(url, newUrl);
}
writer.Write(html) ;
}
}
该代码实现了复写page的rander方法,给解析出来的超链接统一添加token标记。页面使用在页面上继承这个类就可以了。