Asp.net MVC creates custom routes
Hi I am trying to create a url that looks like this:
black / granite / countertops
where black and granite change, so I tried to create my own routes in global.asax.cs like this:
routes.MapRoute("Kitchen", "kitchen/[color]/[surface]/[type]",
new {controller = "kitchen", action = "surface"});
changing url to kitchen / black / granite / countertops
this way i thought i could create a controller called kitchen with an action called surface my code for this looks like this:
public ActionResult surface(string color, string surface, string type)
{
ViewData["color"] = color;
ViewData["surface"] = surface;
ViewData["type"] = type;
return View();
}
however i cant get it to work, i get 404 error for this url despite my custom mapping, can anyone point me in the reading direction i read this page here: http://weblogs.asp.net /scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
here is what gave me the idea, since it has a request and a page, the code is a bit outdated as I am using MVC 2 preview
many thanks
source to share
Now it works in your global.asax, you need something like this:
routes.MapRoute("Kitchen Surface Route",
"kitchen/{color}/{surface}/{type}",
new {controller = "kitchen", action = "surface", color="", surface = "", type=""});
And then you will have ActionLink:
<%= Html.ActionLink("Link Text", "Kitchen", "surface", new {color="theColor", type="theType", surface="surfaceType"}, null) %>
Sometimes this can be somewhat complicated. You can also use Phil Haack Route Debugger to help you.
source to share