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

发布于 2024-08-02 15:09:03 字数 1022 浏览 6 评论 0原文

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

black/granite/worktops

,其中黑色和花岗岩会发生变化,因此我尝试在 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,任何人都可以指出我的阅读方向,我一直在此处阅读此页面:< a href="http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx" rel="nofollow noreferrer">http ://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

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

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福不弃 2024-08-09 15:09:03

它现在的工作方式是在你的 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 的路由调试器来帮助你出去。

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.

—━☆沉默づ 2024-08-09 15:09:03

查看 Phil Haack 的路由调试器来帮助您了解每个请求正在使用哪个路由。

Check out Phil Haack's Route Debugger to help you see which Route is being used for each request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文