Optimizing your route collection for URL generation in ASP.NET MVC (and more!)

If you're used ASP.NET MVC or ASP.NET Dynamic Data, you've using ASP.NET's new Routing feature. ASP.NET Routing is the feature that lets you easily create so-called "pretty" URLs. Pretty URLs are the ones that look the way you want them to look (e.g. ~/products/beverages), not the way your web application platform forces them to look (e.g. ~/products.aspx?categoryname=beverages).

Many people, including myself, have blogged about the various features of Routing, but this post is about optimizing your routes to get the most out of them.

Here are some tricks to make URL generation more performant in your application:

1. Use named routes

Named routes are an optional feature of routing. The names are only used for URL generation; they are never used for matching incoming URLs. When you specify a name when generating a URL we will only try to match the one route with that name. This means that even if the named route you specified is the 100th route in the route table we'll jump straight to it and try to match. Otherwise we'd try every single route until we get to the 100th route. Here's an example of using named routes in an MVC application:

// Global.asax.cs

routes.MapRoute(
    "products-route",
    "products/{category}",
    new {
        controller = "products",
        action = "category",
    });

// Views/Home/Index.aspx

<%= Html.RouteLink("Show Beverages", "products-route", new { category = "beverages" }) %>

Note that if we hadn't specified the name route, we would probably get a different route to match. Also, because we used the named route, we didn't have to specify any extra route values such as the controller or action name.

2. Put your most common routes at the beginning of the route table

This will improve performance of both URL generation as well as processing incoming URLs. The Routing system works on a very simple rule: the first match wins. If the first match happens to be the 100th route in your route table, then that means it had to try 99 other routes and none of them matched. By putting your most common routes towards the top of the route collection, they will match sooner.

However, be careful: Changing the order of your routes can affect how they match. Only change the order of the routes if you're sure that it won't cause a conflict.

3. Don't use URL generation smile_sad

Some people like URL generation and some people don't. It's a bit tricky to master URL generation. It's nice to use URL generation if your URLs are very dynamic and you don't want to update your whole site when you make changes to your URL structure. But it can be a bit of a hassle when you have very few URLs to begin with and perhaps you don't care about exactly what they look like.

My favorite

My favorite option is #1, using named routes, primarily since it's super easy to use. It also makes URL generation more deterministic from the app developer's perspective (that's you!). By giving things names and then specifying exactly what you won't, it eliminates the guessing game of routes.

No Comments