Under the Hood: How Routes Generate URLs

So far, this chapter has focused mostly on how routes match incoming request URLs, which is the primary responsibility for routes. Another responsibility of the Routing system is to construct a URL that corresponds to a specific route. When generating a URL, a request for that generated URL should match the route that was selected to generate the URL in the first place. This allows Routing to be a complete two-way system for handling both outgoing and incoming URLs.

Product Team Aside
Let's take a moment and examine those two sentences. “When generating a URL, a request for that generated URL should match the route that was selected to generate the URL in the first place. This allows Routing to be a complete two-way system for handling both outgoing and incoming URLs.” This is the point where the difference between Routing and URL rewriting becomes clear. Letting the Routing system generate URLs also separates concerns between not just the model, the view, and the controller, but also the powerful but silent fourth player, Routing.

In principle, developers supply a set of route values that the Routing system uses to select the first route that is capable of matching the URL.

High-Level View of URL Generation

At its core, the Routing system employs a very simple algorithm over a simple abstraction consisting of the RouteCollection and RouteBase classes. Before digging into how Routing interacts with the more complex Route class, let's ...

Get Professional ASP.NET MVC 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.