您的位置:首页 > 其它

RewritePath() 实现简单高效的URL重写。

2008-05-30 12:50 330 查看
//原始的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); //关键步骤
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: