MVC Route Map Got Exception on Html.RenderAction : No route in the route table matches the supplied values

六眼飞鱼酱① 提交于 2019-12-02 05:13:05

I have already used this syntax with MVC5 and it works :

        routes.MapRoute(
            name: "HomePage",
            url: "",
            defaults: new { controller = "Home", action = "Index" }
            );

This syntax allows you to access your home page using www.mydomainaddress.com but it won't work if you try using www.mydomainaddress.com/home/index

I don't see why it doesn't work for you. You can try rearanging the order or the MapRoutes definitions (place the "HomePage" route after the "randomNumber" route)

I saw your modified question. I understand you want to be able to use any action for the home controller and don't want to be able to use the randomNumber for the HomeController.

This probably will fit you better (also disabling the randomNumber route for Home controller) :

        routes.MapRoute(
               name: "HomePage",
               url: "",
               defaults: new { controller = "Home", action = "Index" }
          );

        routes.MapRoute(
                name: "HomeActions",
                url: "Home/{action}",
                defaults: new { controller = "Home", action = "Index" }
            );

        routes.MapRoute(
           name: "randomNumber",
           url: "{controller}/{randomNumber}/{action}",
           defaults: new { },
           constraints: new { randomNumber = @"\d+", controller = @"^((?!Home).)*$" }
       );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!