ASP.NET MVC – Routing

ring-tailed-lemur One of the core features of ASP.NET MVC that makes everything “just work” is the concept of routing.  By specifying ahead of time what a route looks like, we can create links that look like regular URLs with no parameters that behave like parameterized URLs on the server.

The magic for this all happens in Global.asax.cs (or .vb if you are using Visual Basic)

Open up the project you created last week and take a look at the Global.asax.cs file and you will find the following code:

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
    );

}

The first line,

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

is saying, “ignore anything that has an AXD extension.

The second line,

 

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

Is saying, “create a route named default that interprets a URL in the form of Directory/Directory/Directory and have it call the Controller class using the first directory name, a method in that controller class using the second directory name and a parameter using the third directory name.  If nothing is passed that matches, then just call the ‘Index’ method of the ‘Home’ controller without any parameters.”

In the sample code, you’ll see that ID is an integer.  But, there is no reason why this parameter could not be a string.  There is also no reason why you couldn’t have multiple parameters if you needed or multiple routes if that makes sense.  You do, however, need at least one route.

You may want to consider NOT using integers for parameters in this scheme.  You may need a lookup table to achieve this but the advantage of not using integers is that your URLs look more “normal” and will be easier for the site visitor to remember.

Which would you rather remember?

http://somesite.com/Recipes/Display/ChickenSalad

or

http://somesite/Recipes/Display/23 ?

 

Other post in ASP.NET MVC

Related Post

  • MVC – Let’s Install it.MVC – Let’s Install it. Unless you only read my blog, you probably already know that MVC 1.0 released yesterday.  So it's time to install it into visual studio. Now, you are supposed to be able to install it using the...
  • ASP.NET Model View ControllerASP.NET Model View Controller Last week the ASP.NET Model View Controller framework was released as Release Candidate 1.  That's my cue to take a look at what we finally have available to us and to start a series explaining ho...
  • ASP.NET MVC – Is The Grass Really Greener?ASP.NET MVC – Is The Grass Really Greener? There are three ways now to write a web site in ASP.NET: Use Classic ASP model with everything in the ASPX file and only use HTML controls without the runat="server" attribute Use the Web Fo...
  • ASP.NET MVC – Controller to ViewASP.NET MVC – Controller to View A couple of weeks ago we looked at ASP.NET MVC routing in the MVC framework.  The routing controls which method in which controller gets called. The obvious next question is, how do we get fr...
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it wrong, which...