asp.net-mvc

Html.RouteLink

Parameters#

Parameter Details
linkText The text that will be displayed for the link.
routeName The name of the route to return a virtual path for.
## Basic Example Using Link Text and Route Name
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.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow