Practical ASP.NET

Routing Your ASP.NET Application

New for ASP.NET developers with .NET 3.5 SP1 is routing, an easy way to simplify site maintenance -- and give meaning to your users' lives.

One of the features added in Service Pack 1 for .NET 3.5 is routing (which first appeared in the MVC version of ASP.NET). Routing allows you to disconnect the physical structure of your site from the URLs that users invoke to access your pages in your site.

At least one benefit of this technology is obvious: You can rearrange your site's structure without invalidating your users' bookmarks. After moving your pages around (or renaming them) you can modify your routing information to point to the new page names. This "routing layer" acts like the data layer in an n-tier application that disconnects business objects from the details of the database structure.

There are other benefits to routing. URLs can be relatively meaningless to users. These URLs, for instance, would be opaque to most users:

http://www.mysite.com/MyApp/InventoryA.aspx?
inventoryid=A1345&action=D
http://www.mysite.com/MyApp/InventoryB.aspx?
category=15&action=L

URLs that would be more meaningful to users might look like these:

http://www.mysite.com/MyApp/Inventory/Details/A1345
http://www.mysite.com/MyApp/Inventory/List/Condiments

To my mind, there's at least one major benefit to "meaningful" URLs: better error 404 messages. If all of your page names are meaningless then, when a user enters an invalid URL, the best you can do is tell them that the page doesn't exist. With a meaningful URL, you can send a page that describes the format of the URL and specifies that the second-to-last parameter must be "Details" or "List" and that the last parameter must be a valid item id or category id. Meaningful URLs also support "URL butchery" where users modify the URL to get the results they want. Users can rewrite their URL and press the Enter key to get the desired results.

In addition to supporting users more effectively, meaningful URLs also can result in improved rankings on search engines. Generally speaking, URLs that contain key search terms will score higher on search engines than meaningless virtual paths.

There's only one piece of bad news: While converting an existing site to using routing is relatively easy, if you use querystrings to pass data between pages you'll need to rewrite some of your existing code. I'll discuss that problem in my next column but in this column, you'll see how to define a route that supports meaningful URLs.

Implementing Routing
The first steps in implementing routing are to add a reference to System.Web.Routing to your Web site and reference the UrlRoutingModule in your web.config file inside the httpModules element, as shown here.

The next step is to add a Global.asax file to your application (cleverly called "Global Application Class" in the Add New Item dialog). You use the Application Start event in the Global.asax file to define the routes in your application by creating Route objects.

When you create a Route object, you specify the template for the URLs your users will provide and the class that will handle converting your URLs into requests for real pages (I'll look at that class in my next column). Once you've created the Route object, you add it to the Routes collection of ASP.NET's RouteTable.

This example creates a Route object, specifying a URL that consists of the word "Inventory," followed by some action, followed by some item id. The code also creates an InventoryRouter object (the class that I'll write to handle URL conversions) and passes that to the Route object.

As you can see, the "replaceable" parts of the URL template are enclosed in braces (these parts are called "URL parameters"). Two parameters must always be separated by some delimiter. Using my previous example as a starting point, I could also have created a template with a hyphen between the action and the item id.

I couldn't just bump the action up to the item id, so this example would be invalid.

Now that I've defined a route, the next step is to decode it. In my next column, I'll return to routing to show how to convert a meaningful URL into an actual path to a WebForm.

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

Subscribe on YouTube