Click here to Skip to main content
11,568,522 members (43,773 online)
Click here to Skip to main content

URL rewriting in ASP.NET 2.0 with simple code sample

, 4 Dec 2008 CPOL 24.1K 1K 27
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vimal Panara
Technical Lead IndiaNIC Infotech Ltd
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
ignatandrei5-Dec-08 1:44
memberignatandrei5-Dec-08 1:44 
GeneralMy vote of 1 Pin
Mustafa J4-Dec-08 20:35
memberMustafa J4-Dec-08 20:35 
QuestionWhat is the point ? Pin
haathi4-Dec-08 14:37
memberhaathi4-Dec-08 14:37 
GeneralMy vote of 1 Pin
l11011004-Dec-08 12:50
memberl11011004-Dec-08 12:50 
GeneralRe: My vote of 1 Pin
Gevorg5-Dec-08 7:50
memberGevorg5-Dec-08 7:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 4 Dec 2008
Article Copyright 2008 by Vimal Panara
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid