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/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube