net MVC创建我自己的路由

时间:2022-11-02 20:53:11

hi i am trying to create a URL that looks like this:

你好,我想创建一个URL,看起来像这样:

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:

那里的黑色和花岗岩会发生变化,所以我尝试在全球创建自己的路线。asax。cs:一样

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

changing the URL to kitchen/black/granite/worktops

将URL更改为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:

通过这种方式,我想我可以创建一个名为kitchen的控制器,它有一个名为surface my code的动作

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

然而,我似乎无法让它工作,我得到了这个URL的错误404,尽管我的自定义映射,任何人都能指出我在阅读的方向,我一直在读这一页:http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-frame -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

这让我有了这个想法,因为他有查询和页面代码有点过时,因为我正在使用MVC preview 2

many thanks

非常感谢

2 个解决方案

#1


1  

The way it works now, is in your global.asax, you'd want something like this:

它现在的运作方式是在你的全球范围内。asax,你会想要这样的东西:

 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:

然后有一个ActionLink,比如

<%= 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.

它有时会变得有些复杂。你也可以使用Phil Haack的路由调试器来帮助你。

#2


1  

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

请查看Phil Haack的路由调试器,以帮助您查看每个请求使用的路由。

#1


1  

The way it works now, is in your global.asax, you'd want something like this:

它现在的运作方式是在你的全球范围内。asax,你会想要这样的东西:

 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:

然后有一个ActionLink,比如

<%= 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.

它有时会变得有些复杂。你也可以使用Phil Haack的路由调试器来帮助你。

#2


1  

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

请查看Phil Haack的路由调试器,以帮助您查看每个请求使用的路由。