FreeKB - MVC Html.ActionLink Helper (hyperlinks)
MVC - Html.ActionLink Helper (hyperlinks)

Classic HTML can produce long, and sometimes unweildy URLs. MVC is much more elegant. Let's start by looking at how HTML handles navigating around different pages. The following is the syntax of the markup to transform the text "Learn more" into a hyperlink that redirects to Content page in classic HTML. In this example, the URL will be

<a href="Content.html"> Learn more </a>


In MVC, the following is the syntax of the markup to transform the text Go into a hyperlink that redirects to a new page. In this example, the URL will be Notice the URL does not end in ".html", yet we still get the Content page.

@Html.ActionLink("Go", "Content")


It is likely that the Content page consists of a default index.cshtml page, such as /Content/Index.cshtml. If we create an ActionLink that direct to these Index page, the web browser may produce a 403 forbidden error.

@Html.ActionLink("Go", "Index", "Content")


To prevent the 403 Forbidden error, we can adding a route to the /Content/Index page in the /App_Start/RouteConfig.cs file:

  new { controller = "Content", action = "Index", id = UrlParameter.Optional }


Now when requesting the /Content/Index page, the URL only displays In another words, the URL does not display as

SQL database

If the destination page gets data from a SQL database, probably will need to appened ID to the ActionLink, which will produce a URL such as To accomplish this, add new {}, null. We can also replace the first field with item.column_name:

@Html.ActionLink(item.column_name, "Index", "Content", new { }, null)


To apply CSS to the URL, add new {@class="sample"} to the end, replacing sample with the name of the CSS class.

@Html.ActionLink(item.column_name, "Index", "Content", new { }, new { @class = "sample" })


To link to an external URL:

<a href="http://@item.url"> Go </a>


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 3de1f in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |