C# asp.net MVC创建我自己的路由

C# asp.net MVC创建我自己的路由,c#,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc Routing,您好,我正在尝试创建一个如下所示的URL: routes.MapRoute("Kitchen Surface Route", "kitchen/{color}/{surface}/{type}", new {controller = "kitchen", action = "surface", color="", surface = "", type=""}); 黑色/花岗岩/工作台面 黑色和花岗岩将发生变化,因此我尝试在

您好,我正在尝试创建一个如下所示的URL:

 routes.MapRoute("Kitchen Surface Route", 
                 "kitchen/{color}/{surface}/{type}",
                 new {controller = "kitchen", action = "surface", color="", surface = "", type=""});
黑色/花岗岩/工作台面

黑色和花岗岩将发生变化,因此我尝试在global.asax.cs中创建自己的路线,如下所示:

 routes.MapRoute("Kitchen", "kitchen/[color]/[surface]/[type]",
                        new {controller = "kitchen", action = "surface"});
public ActionResult surface(string color, string surface, string type)
    {
        ViewData["color"] = color;
        ViewData["surface"] = surface;
        ViewData["type"] = type;
        return View();
    }
将URL更改为kitchen/black/granite/Worktop

通过这种方式,我想我可以创建一个名为kitchen的控制器,其中包含一个名为surface的操作 我的代码如下所示:

 routes.MapRoute("Kitchen", "kitchen/[color]/[surface]/[type]",
                        new {controller = "kitchen", action = "surface"});
public ActionResult surface(string color, string surface, string type)
    {
        ViewData["color"] = color;
        ViewData["surface"] = surface;
        ViewData["type"] = type;
        return View();
    }
然而,我似乎无法让它工作,我得到了错误404这个网址,尽管我的自定义映射,有人可以告诉我阅读的方向,我一直在阅读这个页面在这里:

这就是我的想法,因为他有查询和页面,代码有点过时,因为我使用的是MVC预览2


非常感谢

它现在的工作方式是在您的global.asax中,您可能希望这样:

 routes.MapRoute("Kitchen Surface Route", 
                 "kitchen/{color}/{surface}/{type}",
                 new {controller = "kitchen", action = "surface", color="", surface = "", type=""});
然后你会有这样一个ActionLink:

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

有时路线会变得有些复杂。您还可以使用帮助解决问题。

签出帮助您查看每个请求使用的路由