C# – asp.net MVC creating the own routes

asp.net-mvcasp.net-mvc-2asp.net-mvc-routingc

hi i am trying to create a URL that looks like this:

black/granite/worktops

where the black and granite will change so i have tried to create my own routes in global.asax.cs like so:

 routes.MapRoute("Kitchen", "kitchen/[color]/[surface]/[type]",
                        new {controller = "kitchen", action = "surface"});

changing the URL to kitchen/black/granite/worktops

this way i thought i could create a controller called kitchen with an action called surface
my code for this looks like so:

public ActionResult surface(string color, string surface, string type)
    {
        ViewData["color"] = color;
        ViewData["surface"] = surface;
        ViewData["type"] = type;
        return View();
    }

however i cant seem to get it to work, i get the error 404 for this URL despite my custom mapping, can anyone point me in the direction of reading, i have been reading this page here: http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

this is what gave me the idea, as he has query and page the code is a little owt of date as i am using MVC preview 2

many thanks

Best Answer

The way it works now, is in your global.asax, you'd want something like this:

 routes.MapRoute("Kitchen Surface Route", 
                 "kitchen/{color}/{surface}/{type}",
                 new {controller = "kitchen", action = "surface", color="", surface = "", type=""});

And then you'd have an ActionLink like so:

<%= Html.ActionLink("Link Text", "Kitchen", "surface", new {color="theColor", type="theType", surface="surfaceType"}, null) %>

It can get somewhat complicated with routes sometimes. You can also use Phil Haack's Route Debugger to help you out.