我希望能够通过一个路由约束声明访问以下内容:
/picks
/picks/{teamID}/{week}
/picks/save/{teamID}/{week}
这不适用于第二个请求:/picks/{teamID}/{week}
routes.MapRoute(
"picks",
"picks/{action}/{teamID}/{week}",
new { controller = "Picks",
action = "Index",
teamID = UrlParameter.Optional,
week = UrlParameter.Optional });
在我看来,行为应该默认为Index
,因为我没有提供,但我假设它实际上是在尝试找到动作{teamID}
(这是一个数字)。
如何让这个约束处理所有3个场景?
答案 0 :(得分:1)
您只需要忽略第一条路线的路径字符串中的操作:
routes.MapRoute(
"picks",
"picks/{teamID}/{week}",
new { controller = "Picks",
action = "Index",
teamID = UrlParameter.Optional,
week = UrlParameter.Optional });
请记住在更通用的路线之上放置更具体的路线。
例如:
"picks/{teamID}/{week}"
"picks/{action}/{teamID}/{week}"
"{controller}/{action}/{id}"
路线按照添加顺序进行尝试。
答案 1 :(得分:1)
查看Phill Haack route debugger。
您还可以添加约束,以便teamId
只有在数字为
routes.MapRoute(
"picks",
"picks/{action}/{teamID}/{week}",
new
{
controller = "Picks",
action = "Index",
teamID = UrlParameter.Optional,
week = UrlParameter.Optional
},
new { teamID = @"\d+" });