通过Global.asax实现简单的url重写
2008-06-02 19:04
615 查看
实现目标
在列表页list.aspx中点击某一条内容后显示该条内容的详细信息,一般通过details.aspx?id=5的方式实现,在url栏通过get方法提交参数,为做到对搜索引擎友好,url地址简单可对其进行重写,如list_5.aspx、或list/5.aspx等达到这样的效果。
编码实现,通过正则表达式匹配查找id,再重写
void Application_BeginRequest(object sender, EventArgs e)
{
string id = null;
Regex rg = new Regex(@"^.+list_/d+.aspx$");
string fullOrigionalpath = Request.Url.ToString();
if (rg.IsMatch(fullOrigionalpath))
{
MatchCollection regs = Regex.Matches(fullOrigionalpath, "list_(?<id>.+?).aspx", RegexOptions.IgnoreCase);
if (regs.Count > 0)
{
id = regs[0].Groups["id"].ToString();
}
Context.RewritePath("/website/details.aspx?id="+id);
}
}
通过页面链接<a href="list_5.aspx" title="">url rewrite</a>,url显示为list_5.aspx,而在details.aspx页面可接受到传递的参数Request.QueryString["id"]。
可以对Application_BeginRequest中的方法做适当的封装,对url参数在web.config中做配置。
在列表页list.aspx中点击某一条内容后显示该条内容的详细信息,一般通过details.aspx?id=5的方式实现,在url栏通过get方法提交参数,为做到对搜索引擎友好,url地址简单可对其进行重写,如list_5.aspx、或list/5.aspx等达到这样的效果。
编码实现,通过正则表达式匹配查找id,再重写
void Application_BeginRequest(object sender, EventArgs e)
{
string id = null;
Regex rg = new Regex(@"^.+list_/d+.aspx$");
string fullOrigionalpath = Request.Url.ToString();
if (rg.IsMatch(fullOrigionalpath))
{
MatchCollection regs = Regex.Matches(fullOrigionalpath, "list_(?<id>.+?).aspx", RegexOptions.IgnoreCase);
if (regs.Count > 0)
{
id = regs[0].Groups["id"].ToString();
}
Context.RewritePath("/website/details.aspx?id="+id);
}
}
通过页面链接<a href="list_5.aspx" title="">url rewrite</a>,url显示为list_5.aspx,而在details.aspx页面可接受到传递的参数Request.QueryString["id"]。
可以对Application_BeginRequest中的方法做适当的封装,对url参数在web.config中做配置。
相关文章推荐
- 通过Global.asax实现简单的url重写
- 通过Global.asax实现简单的url重写
- 通过Global.asax实现简单的url重写
- 设置Global.asax实现简单的Url重写
- 使用Global.asax实现ASP.Net的URL重写
- 在global.asax中实现URL重写(应用)
- 设置Global.asax实现简单的Url重写
- Global.asax的Application_BeginRequest实现url重写无后缀的代码
- 使用Global.asax实现ASP.Net的URL重写
- Global.asax的Application_BeginRequest实现url重写无后缀的代码
- 在global.asax中实现URL重写(应用)
- 转:Global.asax通过Application_BeginRequest()事件实现访问链接的静态地址映射
- Global.asax实现URL重写
- Structure.Hash(通过数组和链表实现一个简单的Hash)
- 通过邮箱找回密码简单实现
- 通过游标简单实现循环 定时任务
- 通过JAVA反射实现简单的ORM,将查询结果封装为对象
- Global.asax文件中实现定时更新
- Nio学习之通过通道实现最简单的文件复制代码