Reputation: 8337
How does:
@Html.ActionLink("MyText","Index","Home")
Translates to:
<a href="/Home">MyText</a>
Whereas before it was printing
<a href="/Home/Index">MyText</a>
After the I added a default action for all controller via:
routes.MapRoute(
"DefaultActionToIndex",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional });
It seems to me that the helper method does more than print whatever was passed in but is able to resolve the route dictionary and figure out that "Index" is not necessary since it was set as default.
The question is how?
Upvotes: 5
Views: 8781
Reputation: 28608
Routing works both ways, for incoming requests and URL generation, it tests the routes in System.Web.Routing.RouteTable.Routes
(in order) with the provided the values (in this case { controller = "Home", action = "Index" }
), and uses the first route that matches. If a parameter has a default value, and the provided value is equal to that default value, it's not included in the URL.
Upvotes: 5