asp.net MVC 创建我自己的路由
您好,我正在尝试创建一个如下所示的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它现在的工作方式是在你的 global.asax 中,你需要这样的东西:
然后你会有一个像这样的 ActionLink:
有时路由会变得有些复杂。您还可以使用 Phil Haack 的路由调试器来帮助你出去。
The way it works now, is in your global.asax, you'd want something like this:
And then you'd have an ActionLink like so:
It can get somewhat complicated with routes sometimes. You can also use Phil Haack's Route Debugger to help you out.
查看 Phil Haack 的路由调试器来帮助您了解每个请求正在使用哪个路由。
Check out Phil Haack's Route Debugger to help you see which Route is being used for each request.