Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

URL rewriting in ASP.NET 2.0 with simple code sample

0.00/5 (No votes)
3 Dec 2008 4  
One of the most popular functionalities in ASP.NET 2.0 is URL Rewriting. One strong reason for using this is better search friendly pages on your site for search engines like Google, Yahoo, Live, Alta-Vista etc.

URLRewriting.JPG

Introduction

One of the most popular functionalities in ASP.NET 2.0 is URL Rewriting. One strong reason for this is better search friendly pages for search engines like Google, Yahoo, Live, Alta-Vista etc. Specifically, URL Rewriting can also make it easier to implant common keywords into URLs of pages in your sites, which can increase the chance of someone clicking your link, so your site can get more traffic.

A conventional page URL is like shown below:

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

After applying URL rewriting, the URL could be like:

http://www.URLRewriteExample.com/India

How to do it in an ASP.NET application

Here, I will show you the easiest way to manage URL rewriting. Write this code in the Application_BeginRequest event of the Global.asax in your application:

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 

The code in Global.asax

Declaring the HTTP variable:

Dim myContext As HttpContext = HttpContext.Current

The static property Current on the HttpContext class can be useful whenever the flow of control leaves the code in your Page derived web form. Using this property, you can reach out and magically grab the current Request, Response, Session, and Application objects (and more) for the request you are servicing.

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

This expression will check the characters to be allowed in the URL string. Characters are allowed to repeat any number of times except a new line which is used to pass a response as a URL string. And, the HTTP request will match these URL string characters with the rewrite_regex variable. See the following code:

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

Here, the variable match_rewrite will be match the URL string with a Regular Expression as defined in the variable rewrite_regex.

Now, we request to check the HTTP path and rewrite it with the HTTP requested path.

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

Rewriting the HTTP path

I have written a simple link logic in the default.aspx page:

<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>

Here, you can see that I have not given any page name on the href property. See the previous code in the Global.asax block. There is code to identify the HTTP header name, like India, America, England, Canada etc., and rewrite the path as a response to the hello.aspx page.

Keep one thing in mind that whatever URL you request must be mapped, and rewrite it with your specific web form path, as I have shown in the Global.asax file.

Now, your application is ready to run. You don’t need to do anything in your web.config file or in your IIS HTTP settings. You can download the complete sample code from the top of this article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here