Freigeben über


Welcome to Talking Points

Starting with this entry I am adding a Talking Points session to our blog intended to give you… Talking Points on a particular topic with an easy to read bulletized approach accompanied (if/when available) with a link to a video from channel-9 or some other Microsoft resource – including links to other blogs.

So today I am going to touch on the new enabling feature of the ASP.NET MVC: ASP.NET Routing.

ASP.NET Routing

This capability is at the heart of a new approach that makes possible the Model-View-Controller pattern for new web applications.

With routing your web application’s URL interface becomes more important. End-users are becoming more competent and expect to see more intuitive patterns that can be manually navigated. With the new ASP.NET Routing engine, you can define a set of routes for your application that map to resources and remove the need to develop URL rewriting logic.

These routes can be used to address any ASP.NET resources, including: WebForms, ASMX services, Dynamic Data, and MVC.

Routing Taxonomy

There are two fundamental concepts that are the basics for ASP.NET Routing: Route, and Route Table/Route Collection. They enable mapping to resources and the generation of URLs

Route:

A Route represents a container for all the metadata/information the routing engine needs to determine whether it satisfies an incoming request. This includes the following:

  • The URL, which can optionally have parameters.
  • Any constraints. These can include constraints against parameters in the URL as well as the actual request. An example of this would be confining a parameter by a regular expression pattern, or requiring the HTTP verb of the incoming request be PUT.
  • Default values for any parameters, either URL parameters, or other parameters that the route or route handler might be looking for.
  • The actual route handler that will translate this route into a resource. The route handler is responsible for creating an HTTP handler that will ultimately serve the request, and respond to the user with the information they wanted.

For the most part you can get away with using the provided Route class to represent your routes, but if you need to extend your routes, you can either inherit from Route, or RouteBase (which is the lowest common denominator).

Because the route is responsible for determining whether it matches the current request, if you wanted to modify the logic that made that determination, you would probably want to create your own route type.

Route Table/Route Collection

At the center of the routing engine is the RouteTable class. This class serves as the repository of your application’s route definitions. It itself contains no functionality, it is simply a container that houses a common RouteCollection, that is exposed through its static Routes property.

The RouteCollection in turns is a list of all routes that have been defined. The RouteCollection implements a composite pattern style where it implement similar functionality as the routes it contains, so that you can perform operations on the collection that are then delegated to each contained route (i.e. URL generation, route matching).

Routing offers two powerful mechanisms:

1. Route to resource mapping

  • ASP.NET Routing allows you to define a set of routes that are in turn mapped to resources.
  • Resources in this sense can be anything addressable: ASPX page, ASMX service, MVC controller, etc.
  • The notion of routing isn’t coupled to a page or a controller; you’re just defining a URL pattern in conjunction additional metadata (i.e. constraints) that can be translated into a resource to serve the request mapped to the route.

2. URL generation

  • ASP.NET Routing allows you to use your route definitions to generate URLs.
  • Now your routes become bi-directional, which offers greater power than just using URL rewriting.
  • Using this feature, the resources in your application can reach out to the route definitions and request URLs they can use to point at other resources within your application, including them.
  • This decouples your application’s resources, from your public URL interface.

Pipeline

A pipeline represents the lifecycle of a request in terms of how ASP.NET Routing will process it:

  • A request comes in and the URL Routing Module intercepts it.
  • The module checks the URL against the list of all routes in the application’s route table.
  • If a route is found, the module gets the route’s route handler.
  • The module then get’s the route handler’s HTTP handler.
  • The HTTP handler is then responsible for serving the request.

To Learn More about ASP.NET Routing check Scott Guthrie’s detailed blog entry. And to see ASP.NET MVC in action watch our own Marc’s developer dinner webcast.

And remember, ASP.NET MVC is *not* a replacement for ASP.NET Web apps, rather it builds on it to offer this new pattern. Check it out!

Joel Reyes

Technorati Tags: ASP.NET MVC,ASP.NET Routing

Comments

  • Anonymous
    February 09, 2009
    PingBack from http://www.alvinashcraft.com/2009/02/09/dew-drop-february-9-2009/

  • Anonymous
    February 09, 2009
    Thank you for submitting this cool story - Trackback from DotNetShoutout

  • Anonymous
    March 01, 2009
    Much has been written about ASP.NET and ASP.NET MVC, but my Talking Points would be incomplete without