【发布时间】:2013-03-14 09:52:07
【问题描述】:
我正在努力理解 MVC3 中的路由。
以前我通常只是避开整个区域并坚持使用丑陋的旧 ?id=1&foo=bar 类型网址。不好看。
这样定义了 4 条路线
routes.MapRoute("Blog", "{controller}/{action}/{PageNumber}/{PostsPerPage}", new { controller = "blog", action = "list", PageNumber = UrlParameter.Optional, PostsPerPage = UrlParameter.Optional });
routes.MapRoute("Code", "{controller}/{action}/{title}", new { });
routes.MapRoute("Id", "{controller}/{action}/{id}", new { });
routes.MapRoute("Default", "{controller}/{action}", new { controller = "home", action = "index" });
我已尝试从最具体到最不具体的顺序排列它们。
第一个“博客”路由运行良好,我可以使用像 /blog/list/2/5 这样的 URL,它可以正确映射到我的控制器。
底部的默认路由也可以正常工作。
但是,如果我有这样的操作方法:
public ActionResult BarX(int id)
{
//some stuff
}
public ActionResult BarY(string title)
{
//some stuff
}
我希望它使用第三条路由并生成像 /foo/barX/3 这样的 URL。
如果我使用
@Html.ActionLink("TEST1", "barX", "foo", new { id = 3 }, null)
生成的网址是
/foo/barx?id=3
同样是为
生成的 URL@Html.ActionLink("TEST2", "barY", "foo", new { title = "test" }, null)
是
/foo/bary?title=test
所以我想我的问题是:为什么他们使用旧的 ?id= 语法而不是 /foo/barx/3 生成 URL?
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-mvc-3 routes asp.net-mvc-routing