HTTP Handlers in .NET 2.0
When I rewrote my web site late last year I knew I wanted to use friendly names for my blog pages instead of using URL parameters. For example, the URL to the post on Binary Trees in C# initially was this:
I wanted something friendlier and I knew this could be done easily with an HTTP Handler in .NET 2.0 but it has been a very long time since I had written one. So I procrastinated.
A few weeks ago I bumped into a blog post by Timothy Khuori that described how easy it was to implement one. The code was not longer than 20 lines of code (including curly braces!) that I felt ashamed I hadn’t implemented it my site. I took Timothy’s code, stuffed it into my project, and voila, now I have friendly URLs for my blog posts. The URL for the Binary Trees in C# post now is:
Behing the scenes, my HTTP Handler still uses my original Blog.aspx page to serve the request but it preserves the original URL that the user submited. I updated Blog.aspx to parse the name of the blog topic to retrieve directly from the URL rather than expect it in the query string.
Beside being more human readable, these kinds of URLs are also better for tracking purposes. Since to the web server the request looks like the request for a specific page for each topic (rather than the generic blog.aspx with a parameter with the topic name) the web server can keep track of how many times each topic is requested. This means that I can get statistics in how popular a blog topic is using the tools that I already use to track how popular some pages from my site are.
Posted on: 3/2/2008 4:26 AM
|