Datasheet
No matter how query string parameters are presented, average users will have a hard time understanding.
Instead, it would be much clearer to write the query string with a nice hierarchical representation, like this:
http://www.someblogsite.com/username/2005/January/31
For a typical visitor of a site, the preceding URL is not too hard to figure out. For example, if users were
to remove the day, they would see all of the entries for the entire month of January.
Even when you don’t have a natural hierarchy or just a single parameter, a URL with a meaningful
name, rather than a query parameter, is still easier for a consumer to understand.
The ASP.NET v1.1 Hack
One of the best resources available for performing URL rewriting in ASP.NET v1.x is Scott Mitchell’s
MSDN article titled “URL Rewriting in ASP.NET” at
msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnaspp/html/urlrewriting.asp
. In this article, Scott describes how to perform
URL rewriting with both HTTP Modules and HTTP Handlers, and explains when each is best to use. He
also builds a reusable URL rewriting engine that uses regular expressions via a configuration file.
The ASP.NET v2.0 Replacement
Rewriting URLs is supported, somewhat, in ASP.NET v2.0 with the urlMappings configuration ele-
ment. Add a new entry to
web.config, similar to this for mapping a URL:
<urlMappings enabled=”true”>
<add url=”~/Articles/AspDotNet/UrlRewriting”
mappedUrl=”~/Articles.aspx?cat=1&id=16” />
</urlMappings>
The url attribute is what the user sees and the mappedUrl attribute describes the actual requested page.
In the preceding
urlMappings element, imagine that there is an articles page that dynamically returns
articles based on category and article identifier. The
url shows the preferred user interface, but the
mappedUrl attribute shows the actual page and parameters that will be requested.
Implementing the URL Mapping Capability
I’ve written an example application to show how to use this capability. It is a variation of the article
viewing concept shown previously, but based on year and month. For example, the articles application
will allow you to select the year and then the month. Each page is displayed with readable URLs that
you can use to navigate the application by simply modifying the page address. Listing 1-6 shows the ini-
tial page of the articles application.
Listing 1-6: Main page that uses readable URLs for identifying years: Default.aspx
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs”
Inherits=”_Default” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
(continued)
15
Hacks Revisited
04_597663 ch01.qxp 4/25/06 9:54 PM Page 15