asp.net-mvc Basic Example Using Link Text and Route Name


Example

As an alternative to using Html.ActionLink to generate links in a view, you can use

Html.RouteLink

To make use of this feature, you need to configure a route, for example:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.MapRoute(
    "SearchResults",
    "{controller}/{action}",
    new { controller = "Search", action = "Results" });
}

Then in a view you can create a link to that route like so:

@Html.RouteLink("Search Results", "SearchResults");

Using RouteLink() is convenient if you end up changing controller names, or action method names, since using Html.ActionLink() means having to change the controller and action method name parameters in the call, so that they match the new names which have been changed.

With RouteLink() you can change the route details in the MapRoute() call, in other words in one location, and any code that is referencing that route via RouteLink() will not be required to change.