I have a the following controller containing one action with a single parameter:
public class HomeController : Controller
{
public ActionResult Index(int? param)
{
return View();
}
}
My MasterPage contains an ActionLink which should point to ‘/Home/Index’:
Html.ActionLink("MyActionLinkText", "Index", "Home")
Now I add a route like this:
routes.MapRoute(
"MyRoute",
"Home/Something/{param}",
new { controller = "Home", action = "Index" },
new { param = "\\d+" });
When I navigate to ‘/Home/Index’ the ActionLink points to ‘/Home/Index’. That’s OK.
But when I navigate to ‘Home/Something/123’ the ActionLink also points to ‘Home/Something/123’.
I would understand that behaviour if I generated the ActionLink this way:
Html.ActionLink("test", "Index", "Home", new { param = 123 }, null)
But since I don’t supply any route values, I don’t understand that behaviour.
What do I have to do, to let the ActionLink always point to ‘/Home/Index’, indepently from the current parameters?
You’ll need to add a more specific route for the “Index/Home” case. Something like this will probably do the trick (add it before your own “MyRoute” route, otherwise it won’t be picked up):