MapRoute 未按预期工作
MapRoute not working as expected
考虑到下面的映射路线,当用户转到 /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 转到 Home 控制器中的 HomeSecure 操作。
编辑2:
public ActionResult Index()
{
return View();
}
[AuthorizeHasAccess]
public ActionResult HomeSecure()
{
return View();
}
根据您提供的信息,我认为任何一条路线都不符合请求。 MVC 将寻找带有 "id" 参数的方法签名,并且您的操作方法具有 none。如果请求确实将您带到 Home 控制器上的 Index 方法,请检查某处的错误处理程序是否捕获该条件,然后将您重定向到 /Home/Index
如果您按如下方式设置路由table,则第一个路由将按要求匹配。
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 }
);
}
考虑到下面的映射路线,当用户转到 /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 转到 Home 控制器中的 HomeSecure 操作。
编辑2:
public ActionResult Index()
{
return View();
}
[AuthorizeHasAccess]
public ActionResult HomeSecure()
{
return View();
}
根据您提供的信息,我认为任何一条路线都不符合请求。 MVC 将寻找带有 "id" 参数的方法签名,并且您的操作方法具有 none。如果请求确实将您带到 Home 控制器上的 Index 方法,请检查某处的错误处理程序是否捕获该条件,然后将您重定向到 /Home/Index
如果您按如下方式设置路由table,则第一个路由将按要求匹配。
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 }
);
}