Quick and Dirty URL Rewriting with VB.NET

As a preface, this is really only a solution for those of us who do not have a dedicated or VPs server. If you have access to the IIS Management console there are much better ways to accommodate the need for URL rewriting.

I am going to demonstrate a simple way to implement this, but there are a number of much more complex ways to utilize this code.

Basic Example

You have an ecommerce site and want to clean up your URLs a bit. A call to products.aspx?ID=123 is not quite as good as widgets.aspx.
No Problem.

The solution is a Global.asax file.

From MSDN:

The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET or by HTTP modules. The Global.asax file resides in the root directory of an ASP.NET application. At run time, Global.asax is parsed and compiled into a dynamically generated .NET Framework class derived from the HttpApplication base class. The Global.asax file itself is configured so that any direct URL request for it is automatically rejected; external users cannot download or view the code in it.

The Global.asax file is optional; if you do not define the file, the ASP.NET page framework assumes that you have not defined any application or session event handlers.

So this file goes into the root of your .NET application.

It starts like this

<script language="vbscript" runat="server"> 
Sub Application_BeginRequest(Sender As Object, E As EventArgs)
......

End Sub
</script>

The Application_BeginRequest is a lot like the page_load event in your standard .NET code except it starts with the application not the individual page.

And as an example if we popped this into our application and made it read

<script language="vbscript" runat="server"> 
Sub Application_BeginRequest(Sender As Object, E As EventArgs)
  response.write ("Hello world")
End Sub
</script>

Then from there on every page that loaded within this application would have the "Hello World" line at the top of the page.

Okay with me so far? Great!

Whats next... well we're talking about rewriting URLs so we need to know how to look at the URL itself.

<script language="vbscript" runat="server"> 
Sub Application_BeginRequest(Sender As Object, E As EventArgs)
  Dim httpContext As System.Web.HttpContext = HttpContext.Current
  Dim currentURL As String = CurrentURL.Request.Path.ToLower()
  response.write (currentURL)
End Sub
</script>

Done, now we are showing the URL in place of Hello World, it's really the path rather than the URL, so we don't have to worry about splitting out a bunch of /'s. We should be seeing the path of the current page relative to the global.asax file or the Application root.

I hope I am not losing you here - it doesn't matter, stick with this and you'll get it in the end.

So we've taken our URL and converted it into a string... so we have "products.aspx?ID=123" at the top of our page and it really isn't helping anybody yet...

All we need to do now is instead of calling "products.aspx?ID=123" let's call "widgets.aspx"... and then all we need to do is tell it that "widgets.aspx" = "products.aspx?ID=123"

Here goes.

<script language="vbscript" runat="server"> 
Sub Application_BeginRequest(Sender As Object, E As EventArgs)
  Dim httpContext As System.Web.HttpContext = HttpContext.Current
  Dim currentURL As String = CurrentURL.Request.Path.ToLower()
    If currentURL.IndexOf("widgets") > 0 Then
      objHttpContext.RewritePath("products.aspx?ID=123")
    else
      objHttpContext.RewritePath(httpContext)
    end if
End Sub
</script>

Thats it. One thing to mention here is that you can really, really expand on this.

clicky - widgets.aspx


Comments

Will
11/23/2005 11:06:57 PM How about awesome? I`ll try this out tomorrow. Do you mean if I syndicate this on my site? All credit will be given of course.
Joe Maddalone 11/23/2005 11:30:54 PM go for it.
Will
11/24/2005 7:18:39 PM This is great! I got it to work today. You`re right, it`s extremely easy!
Will
12/8/2005 12:57:58 PM I think now it`s time to look into an XML-based ip blacklisting application. :-)
Sam
8/26/2006 3:10:11 PM This is what I needed to complete the job at hand... Thanks...

Leave a Comment

 

 







LATEST POSTS

PostBackUrl Equals Change the Friggin' Form Action . Curse you DBNull and/or Is Nothing! . Career Developers are Shitbags they are. Consolas Makes Me Geek-ier MS Consolas Font Abuse Things I Read Today and You Should Too Whilst searching for solutions I happened upon...

ADS

MOST POPULAR

Multiple IEs in Windows Date Image Thing Firefox Vs. The World Who Is Xperya? ActionScript Form Fields Quick Watermark IE 7 beta 2 standalone

IP Address Tool Chicago Web Design Free Text Messaging