Adding rel and title to ASP.NET MVC Action Links

RSolberg picture RSolberg · Feb 2, 2010 · Viewed 8.1k times · Source

I decided primarily for SEO reasons to add the "rel" to my action link, but am not sure that the way I have gone about this is going to follow "best practices." I simply created a new Extension method as shown below.

Is this the best way to go about doing this? Are there things that should be modified in this approach?

VIEW

<%= Html.ActionLink("Home", "Index", "Home")
    .AddRel("me")
    .AddTitle("Russell Solberg")
%>

EXTENSION METHODS

public static string AddRel(this string link, string rel)
{
    var tempLink = link.Insert(link.IndexOf(">"), String.Format(" rel='{0}'", rel));
    return tempLink;
}

public static string AddTitle(this string link, string title)
{
    var tempLink = link.Insert(link.IndexOf(">"), String.Format(" title='{0}'", title));
    return tempLink;
}

Answer

Richard Garside picture Richard Garside · Feb 2, 2010

You can add any extra html parameter very easily and don't need to write your own extension methods

<%= Html.ActionLink("Home", "Index", "Home", null,
                     new { title="Russell Solberg", rel="me"}) %>