Practical ASP.NET
Decoding Routes
You can use ASP.NET Routing to decode meaningful URLs into real physical path names.
In my previous column ("
Routing
Your ASP.NET Application"), I talked about how wonderful the routing
feature included in SP1 for .NET 3.5 is, and also showed how to set up a site
to use routes. In this column, I'm going to wrap up that part of the topic by
looking at the class file that you must create to handle the conversion between
the meaningful URL provided by the user and the real path to your WebForm.
When setting up routes, you must specify in your Global.asax file the template
for your users' "meaningful" URLs and the class that you'll use to
convert those meaningful URLs to a physical page name. Listing 1
was my example.
To create the InventoryRouter class that I used in the example, I must add
a class file to my Web site and have it implement the IRouteHandler interface.
That interface contains a single function called GetHttpHandler so the skeleton
of my class looks like Listing 2.
As you can see, this function must return some object that implements the IHttpHandler
interface. Assuming that what you want to return is the reference to some page,
the easiest way to create this object is to pass the virtual path to your WebForm
to the System.Web.Compilation.BuildManager's CreateInstanceFrom method. You
must also pass the type of the object that you want to create; for a WebForm,
that type is the Page class.
Listing
3 creates and returns an IHttpHandler reference based on the InventoryA.aspx
page.
Decoding the Request
What's missing at this point is some process for converting the meaningful URL
that the user provided into a value that I can pass to the CreateInstanceFromVirtualPath
method. The requestContext object that's passed into the GetHttpHandler routine
provides you with the information you need to decode your meaningful URL.
The most useful member on the requestContext parameter is the RouteData property
which has a Values collection. You use the Values collection to retrieve the
values from the URL your user provided. In my example, I used this template
which has two URL parameters (action and itemid):
Inventory/{action}/{itemid}
So a user might provide this URL to get the details on part A1234, effectively
putting "Details" in the action parameter and "A1234" in
the itemid parameter of my template:
http://www.mysite.com/MyApp/Inventory/Details/A1234
Here's the bad news: My existing pages expect to be passed an itemid and an
action (either "D" or "L") in their querystring. Unfortunately,
there's no way to set the querystring from the GetHttpHandler method so I have
to pass that data using some other mechanism. The easiest way is to store the
parameters from the URL in the Context object's Items collection. The code in
Listing
4 retrieves the values from the URL and puts the results in the Context
object's Items collection:
As I said, my existing pages expect to retrieve their data from the QueryString.
So, in the InventoryA.aspx, I have this code to retrieve values from the querystring:
Dim action As String = Me.Request.QueryString("action")
Dim id As String = Me.Request.QueryString("id")
Because my data is now in the Context object, I must replace that code with
code like this:
Dim action As String = Me.Context.Items("action")
Dim id As String = Me.Context.Items("id")
If you're feeling brave, you can make these changes using a global search-and-replace.
However, with that one change to existing code, I have disconnected the physical
location of my site's pages from the URLs that my users provide -- and I've
also given my users URLs that mean something to them.
In my next column, I'll be returning to Routing to look at two topics: specifying
what counts as a valid URL and generating URLs to return to your pages (for
instance, in the href attribute of a hyperlink). Among other changes, I'll eliminate
the need for the Case Else statement that handles unexpected values in the action
parameter.
About the Author
Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.