labs.insert-title.com

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






Discussion

Will

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

go for it.

Will

This is great! I got it to work today. You`re right, it`s extremely easy!

Will

I think now it`s time to look into an XML-based ip blacklisting application. :-)

Sam

This is what I needed to complete the job at hand... Thanks...

New comments are currently disabled, you can find me @joemaddalone Comments temporarily disabled for now, you can find me @joemaddalone