2

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

黑色/花岗岩/台面

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

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

将 URL 更改为 kitchen/black/granite/worktops

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

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

但是我似乎无法让它工作,尽管我有自定义映射,但我得到了这个 URL 的错误 404,任何人都可以指出我的阅读方向,我一直在这里阅读这个页面:http ://weblogs.asp.net /scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

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

非常感谢

4

2 回答 2

1

它现在的工作方式是在你的 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) %>

有时路线会变得有些复杂。您还可以使用Phil Haack 的 Route Debugger来帮助您。

于 2009-08-20T16:21:03.557 回答
1

查看Phil Haack 的 Route Debugger以帮助您了解每个请求正在使用哪个 Route。

于 2009-08-20T16:41:34.170 回答