4

我想要发生的是,如果用户导航到类似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 而不是 Default

注意我试过把路线放在后面,但它会抛出一个错误

4

1 回答 1

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" }
     );
于 2013-01-04T14:38:03.890 回答