Create a gist now

Instantly share code, notes, and snippets.

Hyperlink helpers for C# ASP.NET MVC 3
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));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment