asp.net制作让搜索引擎可以友好访问的链接

来源:互联网 发布:淘宝客服推销术语大全 编辑:程序博客网 时间:2024/04/29 23:26

很多的时候我们在进行查询的时候,总是会以这样的链接方式传递参数:

http://www.yoursite.com/query.aspx?typeid=2

这个链接大家看得很明白,就是我希望查看typeid=2的相关记录的信息。

但是这有个问题就是,搜索引擎的spider进行URL解析困难,因为它不太会理解这种带有参数的方式。

如果希望,每个typeid所对应的数据库查询的结果都能够被搜索引擎轻易的进行收录,我们或许需要写成这样的连接方式。

http://www.yoursite.com/pagetype1.aspx

http://www.yoursite.com/pagetype2.aspx

依次类推。

按照通常的想法,这就需要写n个这样的页面了,相当的繁琐。

不过在ASP.NET中,可以利用Application的BeginRequest的事件进行URL的转换[将静态的页面url的形式转换为带参数的动态页面url],就可以轻易的解决这样的问题了。请看如下的代码:

protected void Application_BeginRequest(object sender, EventArgs e)

{
       HttpContext incoming = HttpContext.Current;
       string oldpath = incoming.Request.Path.ToLower();
       string pageid; // page id requested
 
       //利用正则表达式对url进行解析
       Regex regex = new Regex(@"page(/d+).aspx",  RegexOptions.IgnoreCase |
                RegexOptions.IgnorePatternWhitespace);
       MatchCollection matches = regex.Matches(oldpath);
 
       if(matches.Count > 0)
       {
                //如果满足条件,则进行改写,生成相应的带参数的url方式。
                pageid = matches[0].Groups[1].ToString();          
                incoming.RewritePath("Process.aspx?pageid=" + pageid);
       }
      
}
接下来就是需要做一个对应的动态页面了。
<%
       string pageid = Request.QueryString["pageid"];
       // Create the page content based on this pageid taken here
%>可以来访问一下我做的测试页面。请自己修改page后面的数字。

http://lealting.europe.webmatrixhosting.net/page11.aspx

http://lealting.europe.webmatrixhosting.net/page22.aspx
 
我想我们使用的blog是不是也使用了这样的机制呢?
 
原文请访问这里。http://www.stardeveloper.com/articles/display.html?article=2004022801