Practical ASP.NET

Reduce Maintenance with Routing in ASP.NET 4

You may have noticed that more and more sites have URLs that mean something rather than describing the path to the page. ASP.NET 4 makes this much easier to implement for your site and may save you from ever having to redirect users again.

You may have noticed that more and more sites have URLs that mean something rather than describing the path to the page. ASP.NET 4 makes this much easier to implement for your site and may save you from ever having to redirect users again.

In a column way back in 2008 I showed how you could replace meaningless URLs in your site like this one:

/mysite.com/customer.aspx/id=132&mode=23 

Instead, you could let your users access pages on your site with meaningful URLS like this:

/mysite.com/customer/vogel/credit_history

You can see an example of a meaningful URL in the URL for this column, if you look in your address bar right now.

This isn't just a "nice thing to do for your users" feature. This technique, called routing, can reduce the cost of changes to your site. Routing hides the real URLs for your site behind these meaningful URLs. If you move a page on your site, you just need to change the mapping between the meaningful URL and the page's location.

If the idea of meaningful URLs appealed to you but just seemed like too much work (it took me three columns to cover the technology back in 2008)... well, ASP.NET 4 considerably simplifies the process.

Mapping URLs to pages
You specify the mappings between your meaningful URLs and your real pages in the Application_Start event of your Global.asax file. First, you need to get access to the RouteCollection object available from the System.Web.Routing.RouteTable class, as this code does:

Dim rtc As System.Web.Routing.RouteCollection
rtc = System.Web.Routing.RouteTable.Routes

Now can add a route to the collection using the collections new MapPageRoute method:

rtc.MapPageRoute("", _
    "Customer/{customerName}/{mode}", _
    "~/customer.aspx")

The second parameter to MapPageRoute describes the URL that the user provides (I'll call it the "routed URL"), while the third parameter specifies the actual page (I'll call it "the target page") that will be sent to the user.

In the source URL, you specify portions of the URL where the user can plug in parameters. They're marked with "{}", as in the "{customerName}" in my example. My example would, for instance, allow my site to accept this URL:

/mysite.com/Customer/Vogel/credit_history 

The result would be a request for the customer.aspx page on my site with "Vogel" in the customerName parameter and "credit_history" in the mode parameter.

The fifth parameter to the MapPageRoute method allows you to provide default values for parameters using a RouteValueDictionary. There is also a fourth parameter, which when set to False turns off some security checking. The example below makes "credit_history" the default value for the mode parameter if the user omits it:

Dim rvd As New System.Web.Routing.RouteValueDictionary
rvd.Add("mode", "credit_history")
rtc.MapPageRoute("", _
               "Customer/{customerName}", _
               "~/Customer.aspx", True, rvd)

A user could now retrieve the credit history for a customer with just this URL:

/mysite.com/Customer/Vogel 

You can provide additional RouteValueDictionarys in the sixth and seventh parameters of the method to specify constraints on the parameter values and parameters that will be ignored when ASP.NET is searching for a route (more on that next week)

Debugging with routed URLs
You'll need to change your debugging technique to test your routed URLs: Pressing F5 will use the "physical" URL for your start page rather than one of your routed URLs. The solution is on the Start Options tab of your site's Properties Pages. Select the 'Start URL' option and then enter one of your 'routable' URLs. Once you enter the URL (with port number) as your start URL, you'll need to select your site in Solution Explorer and, in the Properties window, set the 'Use dynamic ports' property to false -- otherwise your start URL will be invalidated the next time you open the site in Visual Studio and a new port number is assigned.

I'm not done yet -- in my next column I'll look at some additional new features added to Routing in ASP.NET 4 and show you how to retrieve the values passed in those parameters.

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
Most   Popular
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.