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

Other Related Items:

Don't Mess With Me... Programmer on Long Sleeve Women's Cotton T-Shirt (in 9 colors)Don't Mess With Me... Programmer on Long Sleeve Women's Cotton T-Shirt (in 9 colors)100% preshrunk heavyweight cotton; double-needle stitching throughout; seamless rib at neck; shoulder-to-shoulder tape; heather grey is 90% cotton, 10% polyester; fashion cut; 5/8" rib collar; fitted tapered sleeve.
Danner Koi NetDanner Koi NetTelescoping up to 6.5'. Designed for Koi. Heavy duty aluminum construction. Soft, fish-safe nylon netting. Sure-grip handle.
Art & Music - Transform your TV Screen into an Art Gallery, and your room into a Symphony Hall - DVDArt & Music - Transform your TV Screen into an Art Gallery, and your room into a Symphony Hall - DVDArt & Music DVD, Vol. 1 Transform your TV Screen into an Art Gallery, and your room into a Symphony Hall! This DVD continuously displays original a... Read More >

If you're new here, you may want to subscribe to the mailing list to get notifications of new post and a virtual tour of past topics. Thanks for visiting!

Related Post

Comments are closed.

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor