C# ASP.NET MVC 3 Hyperlink Helper

The other day whilst tidying the views in my personal ASP.NET MVC 3 (C#) project I ended up developing a HTML Helper that creates external hyperlinks, in an attempt to purge my views of hard-coded anchor tags. I've provided an example of it being used, along with a link to the source-code for anyone who is interested.

The HyperlinkExtensions class consists of two extension methods for the HtmlHelper class; allowing the Helpers to work just like the standard HTML Helpers.

Example usage

@Html.Hyperlink("https://eff.org/", "EFF",
    new Dictionary<string, object>()
    {
        {"title", "Electronic Frontier Foundation"}
    }
)
// Output: <a href="https://eff.org/" title="Electronic Frontier Foundation">EFF</a>

Download

View the Hyperlink Extensions class.

Configuration

If you're unsure how to configure your project to use the class then follow the steps below.

  1. Add the class to your project

  2. Add its namespace to the Pages element in your project's web.config file. Like so...

    <configuration>
      ...
      <system.web.webPages.razor>
    	...
    	<pages pageBaseType="System.Web.Mvc.WebViewPage">
    	  <namespaces>
    		...
    		<add namespace="ExampleProject.Helpers" />
    	  </namespaces>
    	</pages>
      </system.web.webPages.razor>
    </configuration>
  3. You should now be able to use the Hyperlink methods in your views.