Join Jess Chadwick for an in-depth discussion in this video How routing finds controller actions, part of ASP.NET MVC 4 Essential Training.
In the previous video I was hopefully able to convince you of just how important URLs are to your site's overall user experience. With that in mind, let's look in this video how you can use the ASP.NET MVC routing framework to control which URLs users will use to access your site. To begin, take another look at the default route that Visual Studio generates when you create a new ASP.NET MVC project. URLs are mapped to routing patterns, such as these, by combining a series of one or more segments delimited with the forward slash character.
Before we continue, it's important to note that route patterns are always relative to the application root. This means that you do not need to specify the site's host name, for example, http://www.example.com, nor should you start with a forward slash, or a tilde just like you would use to specify a relative URL within your site. The routing engine will throw an exception if it comes across routes that break these rules. Route segments can either be placeholders that map the value that appears in the segment of URL to a variable, or they can be literal values that do not get mapped to any variable, or a combination of the two.
Literal URL values are exactly what they sound like. They look for specific characters in the URL. Literal URL values are also not case-sensitive. For instance, the literal URL pattern login matches both of these URLs, even though one of them spells login with a lowercase L and the other one uses a capital L. Placeholders segments are indicated by wrapping the variable name in curly braces. For instance, the default route contains three placeholder segments, controller, action, and ID.
When the routing engine parses a URL, it extracts the values at each placeholder position, and uses them to populate an instance of the route data class. This class maintains a dictionary of all the important values contained in the route, and the ASP.NET MVC framework uses these values in a number of ways, not the least of which is determining which controller action to execute. In the examples you've seen so far, each segment of the URL contains a single placeholder delimited by a forward slash. However, it's quite possible to create URL parts that contain multiple placeholders by separating each placeholder with a string constant.
For instance, the URL pattern shown here breaks one single URL segment into three different placeholders named param1, param2, and param3. When put into action, this parameter is able to parse this URL into these values. As these routing pattern examples demonstrate, route patterns can be as short or as long as you'd like. Likewise, both literal values, and placeholders can live pretty much anywhere within the URL. So, all three of these examples represent valid routing patterns.
It's also important to know that values added to the dictionary are converted to strings by default. In the case of the default route, this means that if the URL contains numbers, such as the URL shown here, the 123 in this case would be saved as a string value instead of a numeric value. This is rarely a problem, however, since ASP.NET MVC offers plenty of helpful ways to convert the strings into whatever type you need as I will demonstrate in later chapters. As your application becomes more complex, you are likely to want to register multiple routes.
When you do this, it is important that you consider the order in which you register them. When the routing engine attempts to locate a matching route, it simply enumerates the collection of routes in the order they were registered, and stops as soon as it finds the first match. This behavior can cause plenty of headaches if you're not expecting it. Consider the following two routes. The first route contains a single placeholder segment, and sets the default value of the controller parameter to Simple. Whereas the second route contains a single constant segment and sets the default value of the controller parameter to Admin.
Both of these routes are completely valid, but the order in which they are mapped may call us unexpected problems because the first route matches just about any value entered, including the value Admin. That means it will be the first to match a URL such as http://example.com/admin. And since the routing engine stops after finding the first match, the second route will never get used. In order to fix this particular problem, all you need to do is switch the order of the routes so that the Admin route is registered first.
Be sure to keep this scenario in mind and consider the order in which you define your custom routes. Another feature of the URL parsing engine in ASP.NET MVC is the ability to specify a catch-all placeholder. Catch-all placeholders are created by placing an asterisk character at the beginning of a placeholder definition, and can only be included as the last segment of a route. Here is an example of a catch-all placeholder that is used in a search scenario to gather raw search terms from the URL.
This route starts out with a literal value, but it could have also used the normal controller and action placeholders just as well. The special part comes at the end, in which I've added a catch-all placeholder to capture everything that follows the literal search URL segment, and put it into a route parameter named query. With this route in place, if the user types-in a URL such as the one shown, the routing engine parses it into the following route data parameters. Notice that the routing engine completely ignored the forward slash within the URL and just included it in the query parameter value rather than treating it as a separate segment.
As the example in this video shows, ASP.NET MVC's routing framework is both powerful and flexible, giving you complete control over your site's URLs. In this video, I showed you how to tell ASP.NET MVC which URLs should map to controller actions in your site. But in the next video, I'll show you how to tell ASP.NET MVC which URLs you want to avoid mapping to your site.
- What is Microsoft ASP.NET MVC?
- Creating your first ASP.NET MVC project
- Understanding the MVC architecture
- Interacting with models and ViewData
- Applying advanced layout techniques
- Routing requests to controller actions
- Validating user input
- Authenticating registered users
- Caching pages
- Reducing bandwidth usage with bundling and minification
- Displaying mobile-optimized content
- Simplifying a complex application with areas
- Configuring and deploying to IIS or Windows Azure