URL Rewriting (Part 2)

URL rewriting can be performed by many techniques. Basically you need to decide the technique according of your requirements.

Every technique has its ups and downs. I tried using the HTTPModule approach by dynamically re-writing the URL.

This technique involves the usage of the  "Application_BeginRequest" event in global.asax file

Whenever a file is requested, this event gets fired, before the requested page events. In the Application_BeginRequest, we need to check the requested URL and on the basis of that, re-write the URL with the HttpContext

Following is the snapshot of the application.



However, there are some limitations.
  1. You cannot use server.transfer or server.execute methods.
    The Server.Transfer require a relative path as well as the Server.Execute. Hense a virtual path within the virtual directory of application server is required.
    You can notice this when you open the Braces to put the argument i.e. the path of the page that it asks for Path, while if you do the same with
    response.redirect, it rather ask you for the URL. Therefore, a dummy address cannot be used with these two methods like what we do with response.redirect.
  2. The normal Hyperlink controls cannot be used, instead, you need to use Linkbuttons or Buttons with response.redirect in their code
    The reason for this is mainly because they hyperlinks doesnt cause the requested URL to change everytime they are clicked,while on the other hand with
    response.redirect command in the code behind of the linkbutton tries accessing a page with its exact path. 

    for example:
    by using the link button you would see the URL in the address bar as
    http://localhost/myapplication/Products/
    next time clicking on the same linkbutton will cause the same URL to be displayed.

    on the other hand Hyperlink is treated differently with the URL rewriting technique
    Hyperlink provides the existing URL in the address bar and the code concatenates another URL rewrite string with it.
    Hense the URL in the address bar would end UP like as follows:
    http://localhost/myapplication/Products/Products/Products ............... and so on with every click.


The project consists of the following files

Global.asax
MasterPage.master
default.aspx
teampage.aspx 
sportspage.aspx
web.config

The master page contains the menus (Four link buttons) each of which redirects to a different page (A dummy URL).

For Example:

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Response.Redirect("~/Cricket/")
    End Sub

    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
        Response.Redirect("~/Football/")
    End Sub

    Protected Sub LinkButton3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton3.Click
        Response.Redirect("~/Tennis/")
    End Sub

    Protected Sub LinkButton4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton4.Click
        Response.Redirect("~/Hockey/")
    End Sub

 

None of the folder (the redirected url or folder) above in the response.redirect, actually exists.

While in the global.asax file following code is to be implemented:

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim strPath As String = Request.Url.ToString()
        If strPath.Contains("Pakistan/") Then
            Context.RewritePath("~/teampage.aspx?TeamID=1")
        ElseIf strPath.Contains("India/") Then
            Context.RewritePath("~/teampage.aspx?TeamID=2")
        ElseIf strPath.Contains("Sri-Lanka/") Then
            Context.RewritePath("~/teampage.aspx?TeamID=3")
        ElseIf strPath.Contains("Cricket/") Then
            Context.RewritePath("~/sportspage.aspx?SportID=1")
        ElseIf strPath.Contains("Football/") Then
            Context.RewritePath("~/sportspage.aspx?SportID=2")
        ElseIf strPath.Contains("Tennis/") Then
            Context.RewritePath("~/sportspage.aspx?SportID=3")
        ElseIf strPath.Contains("Hockey/") Then
            Context.RewritePath("~/sportspage.aspx?SportID=4")
        Else
            Context.RewritePath("~/default.aspx")
        End If
    End Sub

 

As you can see that, upon detecting the requested URLs which are dummy to show to the user, the context.rewritepath() method to actually determine that what is the underlying page to be redirected.

The querystrings which are not visible to the user in the address bar are used the same way as they are to be used.

You can download the code by clicking on the following link.URL Rewriting Demo

------------------------

There are other ways for the URL rewriting two, while IIS7 provides much better functionalities supporting the pageless URL re-writing

You can have a look at ScottGu's Blog to have an indepth view of what other ways are there for URL re-writing as well.
Othere Useful Links are
http://www.asp101.com/articles/matteo/urlrewriting/default.asp
http://www.mattcutts.com/blog/asp-net-2-and-url-rewriting-sometimes-harmful/

Cheers !

raheel Hussain


Published Monday, July 23, 2007 8:51 PM by Raheel Hussain
Filed under: ,

Comments

No Comments
Powered by Community Server (Non-Commercial Edition), by Telligent Systems