Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC ActionLink omits action when action equals default route value

I have the following routes defined for my application:

routes.MapRoute(
    "Referral", // Route name
    "{referralCode}", // URL with parameters
    new { controller = "Home", action = "Index" } // Parameter defaults
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}", // URL with parameters
    new { controller = "Home", action = "Index" } // Parameter defaults
);

And I'm trying to create an ActionLink to go on the Index action on my AdminController:

@Html.ActionLink("admin", "Index", "Admin")

However, when the view is executed the ActionLink renders as (Index action value is omitted):

<a href="/Admin">admin</a>

Normally this would be ok, but it's causing a collision with the "Referral" route.

NOTE: If I instead use ActionLink to render a different action like "Default," the ActionLink renders correctly:

<a href="/Admin/Default">admin</a>

The fact that the "Default" action renders correctly leads me to believe the problem has to do with the default value specified for the route. Is there anyway to force ActionLink to render the "Index" action as well?

like image 479
rjygraham Avatar asked Dec 31 '10 17:12

rjygraham


1 Answers

Remove the default action parameter on your Default route:

routes.MapRoute(
    "Default", 
    "{controller}/{action}", 
    new { controller = "Home"} //  action omitted
);

That will force the action to always be specified in the url.

like image 142
Keltex Avatar answered Oct 09 '22 09:10

Keltex