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


  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube