适合.net 2.0 的url重写
来源:百度知道 编辑:UC知道 时间:2024/05/16 08:53:01
id的值是数据库的的id字段,该怎么确定啊?
ASP.NET 2.0 重写URL是非常方便的,举例说明一下
此例可实现 xxx-123.html 到 xxx.aspx?id=123 的重写,比如
news-123.html -> news.aspx?id=123
question-234.html -> question.aspx?id=234
一、
在解决方案资源管理器中选择到你的网站,添加新项,选择“全局应用程序类”就是 Global.asax 文件。一个网站只能有一个 Global.asax 文件。
二、
在 Application_Start 函数内容,如下:
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
string oldurl = HttpContext.Current.Request.RawUrl;
string pattern = @"^(.*)-([\d]+).html$";
string replace = "$1.aspx?id=$2";
if (Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled))
{
string newUrl = Regex.Replace(oldUrl, pattern, replace, RegexOptions.IgnoreCase | RegexOptions.Compiled);
Context.RewritePath(newUrl);
}
}
在ASP.NET2.0中实现URL重写
本文参考了网上已有代码,在此基础上进行了整理归纳,总结出在ASP.NET2.0环境下实现URL重写的行之有效的方法。如果转载,请注明出处:雪尘的专栏
一、在网站中添加