MVC3路线与不同的参数类型 [英] MVC3 Routes with Different Parameter Types
问题描述
我努力让我周围的路由头MVC3。
I am struggling to get my head around routing in MVC3.
previously我一般都只是避免了整个地区,并坚持与丑陋的老 ID = 1&安培;富=栏
键入的URL。不是很好。
Previously I have generally just avoided the whole area and stuck with ugly old ?id=1&foo=bar
type urls. Not nice.
我有4个途径正是如此定义
I have 4 routes defined thusly
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" });
我试图从最具体责令其最少的。
I have tried to order them from most specific to least.
第一个'博客'路线工作正常,我可以用这样一个网址 /博客/列表/ 2/5
并正确映射到我的控制器。
The first 'blog' route works fine and I can use a URL like /blog/list/2/5
and it maps correctly to my controller.
在底部的默认路由也工作,我期望的那样。
The default route at the bottom is also working as I would expect.
但是,如果我的操作方法是这样的:
However if I have action methods like this:
public ActionResult BarX(int id)
{
//some stuff
}
public ActionResult BarY(string title)
{
//some stuff
}
我希望它使用的第三条道路,并产生像一个网址 /富/ barX / 3
。
然而,如果我用
@Html.ActionLink("TEST1", "barX", "foo", new { id = 3 }, null)
产生的URL是
/foo/barx?id=3
同样,对于生成的URL
Similarly the URL generated for
@Html.ActionLink("TEST2", "barY", "foo", new { title = "test" }, null)
是
/foo/bary?title=test
所以我想我的问题是:为什么他们生产的URL与旧 ID =
语法,而不是 /富/ barx / 3
?
推荐答案
您所有的路线基本一致。他们是
All of your routes are basically the same. They are
{controller}/{action}/{param}/{param}
我说的一样,因为路由引擎想不明白{控制器} / {行动} / 之间的差异{ID}
和 {控制器} / {行动} / {标题}
和路由引擎真的只是看到
and the route engine would really just see
{controller}/{action}/{PageNumber}/{PostsPerPage}
和混淆所有与第一个路由。
and confuse all the routes with the first one.
一旦路由引擎看到在顶部的几乎通用的 {可选} / {可选} / {可选} / {可选} /
路线,任何与4个或更少如此这般没有进一步的元素可以适应它。
Once the route engine sees your almost generic {optional}/{optional}/{optional}/{optional}/
route at the top, anything with 4 or fewer elements can fit it so it goes no further.
如果,在另一方面,你的路由具有与众不同的开端,而不是一般 {控制器}
:
If, on the other hand, your routes had distinctive beginnings, rather than generic {Controller}
:
routes.MapRoute("Blog", "Blog/{PageNumber}/{PostsPerPage}", new { controller = "blog", action = "list", PageNumber = UrlParameter.Optional, PostsPerPage = UrlParameter.Optional });
routes.MapRoute("Code", "Code/{title}", new { controller = "code", action = "list", title = UrlParameter.Optional });
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "home", action = "index", id = UrlParameter.Optional });
现在,每一个路由引擎看到时间博客/...
它知道,只有1路可以比拟。与所有其他人,它将在寻找匹配移动。每当它看到 code /...
,再次只有1路线匹配。和默认将处理像 {任何控制器} / {行动} / {ID}
不具有博客
或 code
作为第一个路由参数。
Now, every time the routing engine sees Blog/...
it understands that only 1 route can match. With all others, it will move on looking for a match. Whenever it sees Code/...
, again only 1 route is a match. And Default will handle anything like {Controller}/{Action}/{id}
that doesn't have Blog
or Code
as a first route param.
这篇关于MVC3路线与不同的参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!