Home » ASP.NET MVC » ASP.NET MVC – Routing

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

  • ASP.NET, Angular.js & html5modeASP.NET, Angular.js & html5mode I’ve been looking at Angular.js recently.  I’ve already got enough of a project done in MongoDB (with Mongoose), Express, Angular and Node.js (MEAN) to be comfortable with how Angular […]
  • 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 […]
  • 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 […]
  • WebForms vs MVC–The War Is OverWebForms vs MVC–The War Is Over loading... I just finished listening to a DotNetRocks podcast today with Paul Sheriff which largely talked about creating mobile web sites using ASP.NET WebForms. During the show they […]
  • 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 […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.