我想要发生的是,如果用户导航到Controller Action
这样的Home/Index
页面并且操作存在,则正常继续。但是,如果用户导航到不存在的Controller Action
Home/NotExist
,那么我想将其默认为名为MyController
的“默认”控制器。
我尝试在接受*url*
routes.MapRoute(
"CustomRoute",
"{*url}",
new { controller = "MyController", action = "MySingleAction" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
但是Home/Index
将转到CustomRoute而不是默认
N.B 我之前尝试过该路线,但却抛出错误
答案 0 :(得分:0)
您应该让自定义路线成为最后定义的路线。在这种情况下:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"CustomRoute",
"{*url}",
new { controller = "MyController", action = "MySingleAction" }
);