When getting started with ASP.NET MVC and/or the
ASP.NET Web API,
it can be overwhelming trying to figure out how it all works. These
frameworks offer powerful features, and abstract away a good deal of
pain associated with handling, routing, and responding to HTTP requests
within an application. This is a great thing for seasoned developers who
understand what it is the framework is doing "for" you (and how to
modify that behavior, if desired). It also makes it easier for new or
less-experienced folk to set up a basic site or API and watch it "just
work."
On the other hand, the abstraction can make it challenging for those
new to the MVC world to understand just what is going on, and where the
critical functionality they want to modify "lives."
One of the fundamental concepts to understand when using ASP.NET MVC
and/or the ASP.NET Web API is routing, which essentially defines how
your application will process and respond to incoming HTTP requests.
Image by Andrew Morton
Important note: This post covers the most basic
and fundamental concepts of routing as applied to the ASP.NET MVC
framework. The target audience are those with little or no familiarity
with routing in general, or who may be looking to review the
fundamentals. If you are an experience MVC developer, there is probably
not much here for you, although your feedback in the comments is greatly
appreciated.
UPDATE 9/26/2013 -
ASP.NET 5.0 and WebApi 2.0 introduce Attribute Routing as a standard
"out-of-the-box" feature. Attribute routing still follows most of the
patterns discussed here, but moves the route definitions out to the
controller methods they service. Attribute routing does not replace the
normal centralized route table discussed here, and in fact there is some contention about
what is the "one true way" to define routes. I will look more closely
at this new feature of ASP.NET in an upcoming post. Suffice it to say
there are differing architectural and design concerns implicit with both
approaches.