【问题标题】:asp.net MVC creating my own routesasp.net MVC 创建我自己的路由
【发布时间】:2009-08-20 16:14:08
【问题描述】:

您好,我正在尝试创建一个如下所示的 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

非常感谢

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-2 asp.net-mvc-routing


    【解决方案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's Route Debugger 来帮助您。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2011-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-24
        • 2011-04-02
        • 2020-04-15
        • 1970-01-01
        相关资源
        最近更新 更多