The basics of ASP.NET MVC routes (excerpt from ASP.NET MVC in Action)

(excerpt from ASP.NET MVC in Action).

The Global.asax.cs file contains some routes that are provided with the MVC Web Application project. Before continuing, we must define what a route is. A route in the ASP.NET MVC Framework is the complete definition for how to handle a web request. With Web Forms, we have little control over this without resorting to url rewriting. With Web Forms, the url of the web request is tightly coupled to the page handling the request. If the page was named Foo.aspx in a folder named Samples, then the url was sure to be http://mvccontrib.org/Samples/Foo.aspx. Many teams have resorted to url rewriting to wrangle some control over the urls and how to satisfy each request. With the ASP.NET MVC Framework, routes are first class citizens in the web application. We start with defining how we want our urls structured. The project template gives us a few routes to start, shown in Listing 1.1.

Listing 1.1 Default routes for a new project. These are added by the project template.

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace GettingStarted
{
    public class GlobalApplication : HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new {controller = "Home", action = "Index", id = ""}
                );
                // Parameter defaults
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

Routes need to be defined as one of the first things when the web application starts up, so the project template adds the routes to the Application_Start method in the Global.asax.cs file. Later in the book, you'll see that we don't leave the routes in this location except for the most trivial of web applications.

Note

We will follow long-standing best practices of Separation of Concerns and the Single Responsibility Principle , or SRP, by moving the routes to a dedication location separated by an interface. We'll go more into these principles later, but, in short, the responsibility (or concern) of the Application_Start method is to kick off operations that must happen at the beginning of the application's life. The responsibility is not to perform every bit of work that must happen on start. Any operations that must happen on application start should reside in separate classes and merely be called in the appropriate order in the Application_Start method.

Note that the url portion of the route is simply a matching mechanism for the request. If the url matches a particular route, then we specify what controller should handle the request and what action method should execute. You can create as many routes as you like, but one route is provided for you. This route has the template, {controller}/{action}/{id}.

Note

This is a very generic route and could be used for many, many different web requests. Tokens are denoted by the inclusion of {braces}, and the word enclosed in braces matches with a value the MVC Framework understands. The most common that we'll be interested in are controller and action. This is the route we will be using for the rest of the chapter, so we will be content with a url in the form of http://mvccontrib.org/controllername/actionname. The basic RouteHandler is an instance of IRouteHandler and we'll use MvcRouteHandler most of the time. We have complete control and could provide our own implementation of IRouteHandler if we wished, but we'll save that for a later chapter.

Before we spin up our first controller, let's examine what is different about the web.config file in an MVC Web Application project. The differences are easy to spot. Just look for “routing” or “mvc”. One difference we see is that a new IHttpModule is registerd in the config file. Here, we see the UrlRoutingModule in listing 1.2

Listing 1.2 Unique addition to the web.config file. The rest of the web.config file is standard for .Net 3.5.

<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

The UrlRoutingModule evaluates a request and sees if it matches a route that is stored in the RouteTable. If the route matches, it overrides the default handler (IHttpHandler) for the request so that the MVC Framework handles the request. We are going to examine our first controller as a means to handle a route for the url /home. In the next section you will see how all the pieces of the starter project fit together.

Stay tuned for more:  My feed:  http://feeds.jeffreypalermo.com/jeffreypalermo

You will also want to pay attention to my co-authors:  Ben Scheirman and Jimmy Bogard.


Trackbacks

Link Listing - September 1, 2008 Posted on 9.02.2008 at 1:32 AM

Link Listing - September 1, 2008

Reflective Perspective - Chris Alcock » The Morning Brew #170 Posted on 9.02.2008 at 2:19 AM

Pingback from Reflective Perspective - Chris Alcock » The Morning Brew #170

Dew Drop - September 2, 2008 | Alvin Ashcraft's Morning Dew Posted on 9.02.2008 at 7:46 AM

Pingback from Dew Drop - September 2, 2008 | Alvin Ashcraft's Morning Dew

Blog Carnival #3 Posted on 9.03.2008 at 12:00 PM

Visual Studio Extensibility/ DSL Tools How to open files from the Output window - by Pablo How do I obtain

ASP.NET MVC Archived Buzz, Page 1 Posted on 9.11.2008 at 10:10 AM

Pingback from ASP.NET MVC Archived Buzz, Page 1

Recent Faves Tagged With "url" : MyNetFaves Posted on 10.31.2008 at 3:50 PM

Pingback from Recent Faves Tagged With "url" : MyNetFaves

New and Notable 271 Posted on 12.02.2008 at 1:38 PM

And that's it, summer is over. ASP.NET MVC ScottGu returns with ASP.NET MVC Preview 5 and Form Posting Scenarios Jeff Palermo has published another excerpt from his upcoming ASP.NET MVC in Action book, this time the Basics of MVC Routes LINQ K. Scott

Comments

mario amatucci said on 1.15.2009 at 7:06 AM

Hi, I would like to propose asp .net mvc in a porting project from traditional ASP, but when it ll be the final release of ASP.net MVC?

Someone knows that?

Thanks

Jeffrey Palermo said on 1.15.2009 at 8:22 AM

@Mario,

I don't know when it will be released, but common hope is by March.