考虑到下面映射的路由,当用户转到/Home时,为什么第二条路由会击中而不是第一条呢?我的假设是,去/Home将导致第一条路线被击中,但出于某种原因,第二条正在被使用。
为什么会这样呢?
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Home page behind auth",
"Home",
new { controller = "Home", action = "HomeSecure", id = "" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}编辑:为了清楚起见,我希望/Home转到主控制器中的HomeSecure操作。
edit2:
public ActionResult Index()
{
return View();
}
[AuthorizeHasAccess]
public ActionResult HomeSecure()
{
return View();
}发布于 2015-07-16 16:02:18
考虑到您所提供的信息,我认为这两条路线都不符合要求。MVC将寻找带有"id“参数的方法签名,而您的操作方法没有。如果请求确实带您到Home控制器上的Index方法,请检查某个错误处理程序是否捕获了该条件,然后将您重定向到/Home/Index
如果按以下方式设置路由表,则第一条路由将按需要匹配。
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Home page behind eRaider",
url: "Home",
defaults: new { controller = "Home", action = "HomeSecure" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", UrlParameter.Optional }
);
}https://stackoverflow.com/questions/31458728
复制相似问题