Saturday 15 November 2014

Routing in MVC

Routing within the ASP.NET MVC framework serves two main purposes:

  •  It matches incoming requests that would not otherwise match a fi le on the file system and maps the requests to a controller action.
  •  It constructs outgoing URLs that correspond to controller actions.

The preceding two items describe only what Routing does in the context of an ASP.NET MVC application. 

NOTE: 
 One constant area of confusion about Routing is its relationship to ASP .NET MVC. In its pre-beta days, Routing was an integrated feature of ASP.NET MVC. However, the team saw that it would have a useful future as a fundamental
feature of ASP.NET that even Web Pages could build on, so it was extracted
into its own assembly and made part of the core ASP.NET framework. The
proper name for the feature is ASP.NET Routing, but everyone simply shortens
it to Routing.
Putting this feature into ASP.NET meant that it became a part of the .NET
Framework (and, by association, Windows). So, while new versions of
ASP.NET MVC ship often, Routing is constrained by the schedule of the larger
.NET Framework; hence, it hasn’t changed much over the years.
ASP.NET Web API is hostable outside of ASP.NET, which means it can’t use
ASP.NET Routing directly. Instead, it introduces a clone of the Routing code.
But when ASP.NET Web API is hosted on ASP.NET, it mirrors all the Web API
routes into the core ASP.NET Routing’s set of routes. Routing, as it applies to
ASP.NET Web API

Comparing Routing to URL Rewriting
To better understand Routing, many developers compare it to URL rewriting. After all, both approaches are useful in creating a separation between the incoming URL and what ends up handling the request. Additionally, both techniques can be used to create pretty URLs for Search Engine Optimization (SEO) purposes.
The key difference is that URL rewriting is focused on mapping one URL to another URL. For example, URL rewriting is often used for mapping old sets of URLs to a new set of URLs. Contrast that to Routing, which is focused on mapping a URL to a resource.
You might say that Routing embodies a resource-centric view of URLs. In this case, the URL represents a resource (not necessarily a page) on the Web. With ASP.NET Routing, this resource is a piece of code that executes when the incoming request matches the route. The route determines how the
request is dispatched based on the characteristics of the URL — it doesn’t rewrite the URL.
Another key difference is that Routing also helps generate URLs using the same mapping rules that it uses to match incoming URLs. URL rewriting applies only to incoming requests and does not help in generating the original URL.
Another way to look at it is that ASP.NET Routing is more like bidirectional URL rewriting.
However, this comparison falls short because ASP.NET Routing never actually rewrites your URL.
The request URL that the user makes in the browser is the same URL your application sees throughout the entire request life cycle.