65.9K
CodeProject 正在变化。 阅读更多。
Home

使用简单代码示例在ASP.NET 2.0中实现URL重写

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.73/5 (10投票s)

2008 年 12 月 4 日

CPOL

2分钟阅读

viewsIcon

39304

downloadIcon

1099

URL重写是ASP.NET 2.0中最流行的功能之一。使用它的一个重要原因是,可以为搜索引擎(如Google、Yahoo、Live、Alta-Vista等)创建更友好的搜索页面。

URLRewriting.JPG

引言

URL重写是ASP.NET 2.0中最流行的功能之一。这样做的一个重要原因是,可以为搜索引擎(如Google、Yahoo、Live、Alta-Vista等)创建更友好的搜索页面。具体来说,URL重写还可以更容易地将常用的关键词嵌入到网站页面URL中,从而增加点击链接的几率,提高网站流量。

传统的页面URL如下所示

http://www.URLRewriteExample.com/hello.aspx?country=India

应用URL重写后,URL可能如下所示

http://www.URLRewriteExample.com/India

如何在ASP.NET应用程序中实现

这里,我将向您展示管理URL重写的最简单方法。将这段代码写入应用程序中Global.asaxApplication_BeginRequest事件。

Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim myContext As HttpContext = HttpContext.Current
    Dim rewrite_regex As Regex = _
        New Regex("(.+)\/((.+)\.*)", RegexOptions.IgnoreCase)
    Try
        'see if we need to rewrite the URL
        Dim match_rewrite As Match = _
          rewrite_regex.Match(myContext.Request.Path.ToString())
        If match_rewrite.Groups(2).Captures(0).ToString() = "All" Then
            myContext.RewritePath("hello.aspx")
        ElseIf match_rewrite.Groups(2).Captures(0).ToString() = "America" Then
            myContext.RewritePath("hello.aspx")
        ElseIf match_rewrite.Groups(2).Captures(0).ToString() = "India" Then
            myContext.RewritePath("hello.aspx")
        ElseIf match_rewrite.Groups(2).Captures(0).ToString() = "England" Then
            myContext.RewritePath("hello.aspx")
        ElseIf match_rewrite.Groups(2).Captures(0).ToString() = "Canada" Then
            myContext.RewritePath("hello.aspx")
        End If
    Catch ex As Exception
        Response.Write("ERR in Global.asax :" & ex.Message + _
                       Constants.vbLf + Constants.vbLf + _
                       ex.StackTrace.ToString() & Constants.vbLf + _
                       Constants.vbLf)
    End Try
End Sub 

Global.asax中的代码

声明HTTP变量

Dim myContext As HttpContext = HttpContext.Current

当控制流离开Page派生Web窗体中的代码时,HttpContext类的静态属性Current非常有用。使用此属性,您可以访问并获取当前请求的RequestResponseSessionApplication对象(以及更多)以进行服务。

Dim rewrite_regex As Regex = _
    New Regex("(.+)\/((.+)\.*)", RegexOptions.IgnoreCase)

此表达式将检查URL字符串中允许的字符。除了用于将响应作为URL字符串传递的新行外,字符可以重复任意次数。并且,HTTP请求将使用rewrite_regex变量与这些URL字符串字符匹配。请参见以下代码

Dim match_rewrite As Match = rewrite_regex.Match(myContext.Request.Path.ToString())

在这里,变量match_rewrite将使用在变量rewrite_regex中定义的正则表达式与URL字符串匹配。

现在,我们需要检查HTTP路径并使用HTTP请求路径对其进行重写。

If match_rewrite.Groups(2).Captures(0).ToString() = "All" Then
    myContext.RewritePath("hello.aspx")
ElseIf match_rewrite.Groups(2).Captures(0).ToString() = "America" Then
    myContext.RewritePath("hello.aspx")
ElseIf match_rewrite.Groups(2).Captures(0).ToString() = "India" Then
    myContext.RewritePath("hello.aspx")
ElseIf match_rewrite.Groups(2).Captures(0).ToString() = "England" Then
    myContext.RewritePath("hello.aspx")
ElseIf match_rewrite.Groups(2).Captures(0).ToString() = "Canada" Then
    myContext.RewritePath("hello.aspx")
End If

重写HTTP路径

我在default.aspx页面中编写了一个简单的链接逻辑

<ul>
<li><a href="All">Hello to All</a></li>
<li><a href="India">Hello to India</a></li>
<li><a href="America">Hello to America</a></li>
<li><a href="England">Hello to England</a></li>
<li><a href="Canada">Hello to Canada</a></li>
</ul>

在这里,您可以看到我没有在href属性中指定任何页面名称。请参见前面Global.asax块中的代码。该代码用于识别HTTP标题名称(例如印度、美国、英国、加拿大等),并将路径重写为对hello.aspx页面的响应。

请记住,您请求的任何URL都必须进行映射,并使用您指定的Web窗体路径对其进行重写,就像我在Global.asax文件中所示。

现在,您的应用程序已准备好运行。您无需在web.config文件中或IIS HTTP设置中进行任何操作。您可以从本文顶部下载完整的示例代码。

© . All rights reserved.