MVC Route Map Got Exception on Html.RenderAction :

2019-03-01 12:01发布

问题:

I use ASP.NET MVC 5 and this is My Route Map for all of the actions except Home/index:

 routes.MapRoute(
                name: "randomNumber",
                url: "{controller}/{randomNumber}/{action}",
                defaults: new { },
                constraints: new { randomNumber = @"\d+" }
            );

And for first page: Home/Index I don't want to use {randomNumber}

So the first solution I think is:

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

This route map solve my problem, but caused another problem that is: clients can access to other actions without {randomNumber}, but I just want the Index action of Home Controller accessed without random number.

The other Solution Is:

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

But with this map I can't access Home/index with root url, I need to access Home/index with root url like this: www.mydomainaddress.com

Finally i found this:

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

But I got the Exception: No route in the route table matches the supplied values. on index.cshtml file in this line: @{Html.RenderAction("ArchiveList", "Home");}

I don't know the ralation of the route map that I added and RenderAction helper?

But If I add both the following two route maps, everything will be OK:

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

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

But I can't add the second map route as you know (I need all actions accessed with random number)

I can't understand what happened when I add the Default map, and what related to RenderAction helper?

Does any one have any idea about this? any suggestion?

回答1:

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).)*$" }
       );