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:


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


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}", _

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:


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:


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

comments powered by Disqus


  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

  • Microsoft Ships Visual Studio 2019 v16.9 Servicing Baseline Release

    Microsoft is urging enterprises and professional coders to standardize on the new Visual Studio 2019 v16.9, a servicing baseline release that's guaranteed to receive official support for an extended period.

  • Microsoft Extends Low-Code Push with Power Fx Programming Language

    "Using formulas that are already familiar to hundreds of millions of users, Power Fx allows a broad range of people to bring skills they already know to low code solutions."

  • Microsoft's Tools to Fight Solorigate Attack Are Now Open Source

    Microsoft open sourced homegrown tools it used to check its systems for code related to the recent massive breach of supply chains that the company has named Solorigate.

Upcoming Events