【发布时间】:2016-10-16 16:48:31
【问题描述】:
在 EuroController 的 Index.cshtml 视图中,我有一个 ActionLink 我想使用 Euro 控制器的“IndexByYear”操作:
@Html.ActionLink("Year 2006", "IndexByYear","Euro", new { id = "", year = 2006 }, null)
但问题是它转到了 Index() 方法,即使它是在 RouteConfig 上设置的所有内容:
routes.MapRoute(
name: "Default",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Euro",
url: "{controller}/{year}/{id}",
defaults: new { controller = "Euro", action = "IndexByYear", year = DateTime.Now.Year, id = UrlParameter.Optional }
);
这是 EuroController:
public ActionResult Index(int? id)
{
...
}
public ActionResult IndexByYear(int? id, int year)
{
...
}
这也不起作用,因为它也适用于 Index() 方法:
@Html.RouteLink("Ano 2006","Euro",new { id = "", year = 2006 },null)
如果我手动导航到 domain/Euro/2016/1,则它使用正确的路线。似乎没有参数它通过默认路由。
我的问题是,为什么 ActionLink 不使用指定的 IndexByYear,或者 RouteLink 使用指定的默认(欧元)路由?
【问题讨论】:
-
交换路线的顺序。
-
如果我这样做,domain/ 会显示 EuroController 的 Index(),而不是 HomeController。
-
那是因为您已经用修改后的版本替换了包罗万象的路线。将
Euro改回Home。如果您只想匹配该特定路由,请创建一个副本,将{controller}占位符替换为euro。 -
所以你的意思是我应该更改默认值:new { controller = "Euro"....} TO 默认值:new { controller = "Home" 。 ...} ?
标签: asp.net-mvc routes actionlink