//原始的URL:http://www.ccol.cn/news/12/66.aspx
//转换后URL:http://www.ccol.cn/news.aspx?q1=12&q2=66
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Regex re = new Regex(@"^((/[^/0-9]+)+)(/[0-9]+(/[^/]+)*)/.aspx$", RegexOptions.Compiled);
Match match = re.Match(HttpContext.Current.Request.Path);
if(match.Success)
{
re = new Regex(@"/([^/]+)", RegexOptions.Compiled);
MatchCollection matches = re.Matches(match.Result("$3"));
string page = match.Result("$1")+".aspx?q0="+matches[0].Result("$1");
for(int i=1; i<matches.Count; i++)
{
page += "&q"+i+"="+matches[i].Result("$1");
}
HttpContext.Current.RewritePath(page); //关键步骤
}
}
本文介绍了一种通过正则表达式匹配和替换来实现URL重写的ASP.NET技术。该技术可以将原始URL转换为更简洁的形式,并展示了具体的实现代码。
1万+

被折叠的 条评论
为什么被折叠?



