ASP.NET Tip 100. Why Do I Use URL Routing? For Ease of Navigation, of course!
For more tips like this, LIVE and IN PERSON, come see us at the MSDN Roadshow !
One of the best new features of ASP.NET WebForms 4.0 is URL Routing, which enables you to change the URL to point to an evaluated expression instead of a fixed file location parameterized with a query string. Why should you care? Read this story and decide for yourself.
This is 100 in a series of ASP.NET tips, which you can subscribe to here: This book of ASP.NET Tips is geared to the ASP.NET developer from novice to advanced. All the tips are categorized by level. Anything below 100 is simple, very easy beginner stuff. 100 Level means that you've worked a little bit with that technology. 200 is intermediate. 300 is advanced, 400 is deep dive. Each Friday, there will be a Living Wiki published (yes I know it's really redundantly repetitive) that contains a multiple of tips on a particular end goal - like being the best demo'er.
Do you really know where your takeout food is coming from?
So in the last 10 years living in NYC, the 2nd most popular site I have used is menupages.com (foremost, the honor goes to NYTimes, naturally). As an avid mouse avoider, I would navigate the site with only my keyboard. I skip around from neighborhood to neighborhood by typing in ALT+D (which takes me to the address bar), and then using my cursor to flip out values in the querystring. I soon came to memorize ?neighborhood=12 and ?neighborhood=11 and ?neighborhood=3 and neighborhood=?4. However, since they’ve changed their design to use intelligent routing, I have soon forgotten what those values mean, and most likely no longer order from those neighborhoods. As a power user, why should I have to memorize arbitrary numbers for neighborhoods ? And we haven’t even gotten into the numbers around cuisines!
Now forget power users - think of it from the perspective of a user. The Address Bar is prominently displayed in any browser and even the most basic user has typed into it. How useful is it for them to see:
Do you really know where your food is coming from when you read? (Ok you might, but what about your mom, she cares what you eat too!)
Now what what if she saw this URL instead? Now she knows you’re a proper Bahn Mi eating hipster:
It’s a simple thing, but wow, how much does it make life friendlier for the average user! Plus, for the power user for me, I now freed up some storage space in my brain that I can use for other things. Like.. hmm.. funny my mind is a blank.
The menupages.com story is a perfect poster child for an example of WHY routing is so important. (And by the way, I did check Netcraft to make sure it’s running on our software – it is!). It made life so much easier for me to type in English words for cuisines and neighborhoods than memorizing neighborhood values and typing into the querystring.
Do you want to improve the search relevancy of your page?
Many search engines will use the URL itself to index the page. So when you choose to embed your product names, categories, neighborhoods into the URL instead of the querystring, you automatically boost your search relevance. Also, by using custom rerouting, you don’t need to have your page bookmarked or indexed with the code file itself (e.g. default.aspx). This way, search indexes will not mistakenly store blogs.msdn.asli and blogs.msdn.asli/default.aspx as two separate entries; really concentrating your page ranking into one entry.
Search Engine Optimization (SEO) is an extensive topic, so if this value proposition is new to you, definitely read more about it.
Ok, I am convinced. How do I use URL Routing?
The easiest example to follow already exists for you! During our roadshow, I plan to demo this solution (thanks to former Evangelist Drew Robbins and his team), however you can download the Visual Studio 2010 and .NET 4 Training Kit for yourself. Be forewarned, it is meant to work with Beta1 of Visual Studio 2010. Once you install it, navigate to the labs for ASP.NET 4.0. It will be Example 2: Routing. Be sure to download the AdventureWorks database in advance.
Or if you are too lazy to code a sample, just do a search on the new PageRouteHandler class.