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://sketchingdev.co.uk/", "Sketching Dev",
new Dictionary<string, object>()
{
{"title", "The Sketching Developer"}
}
)
// Output: <a href="https://sketchingdev.co.uk/" title="The Sketching Developer">SketchingDev</a>
Hyperlink Extensions class
namespace ExampleProject.Helpers
{
using System;
using System.Collections.Generic;
using System.Web.Mvc;
public static class HyperlinkExtensions
{
public static MvcHtmlString Hyperlink(this HtmlHelper helper, Uri url, string text = null, IDictionary<string, object> htmlAttributes = null)
{
if (url == null)
{
throw new ArgumentNullException("url");
}
return HyperlinkExtensions.Hyperlink(helper, url.AbsoluteUri, text, htmlAttributes);
}
public static MvcHtmlString Hyperlink(this HtmlHelper helper, string url, string text = null, IDictionary<string, object> htmlAttributes = null)
{
if (url == null)
{
throw new ArgumentNullException("url");
}
TagBuilder builder = new TagBuilder("a");
builder.Attributes.Add("href", url);
if (text != null)
{
builder.SetInnerText(text);
}
if (htmlAttributes != null)
{
builder.MergeAttributes(htmlAttributes);
}
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
}
}
}
Configuration
If you’re unsure how to configure your project to use the class then follow the steps below.
- Add the class to your project
- Add its namespace to the Pages element in your project’s
web.config
file - below. - You should now be able to use the Hyperlink methods in your views.
<configuration>
...
<system.web.webPages.razor>
...
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
...
<add namespace="ExampleProject.Helpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>