Adding rel and title to ASP.NET MVC Action links

First of all, I decided to add “rel” to my link to the action for SEO reasons, but I’m not sure that the way I went about it will follow the “best practices”. I just created a new extension method as shown below.

Is this the best way to do this? Is there something that needs to be changed in this approach?

BROWSE

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

EXPANSION 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; } 
+7
asp.net-mvc canonical-link actionlink
source share
3 answers

You can add any additional html parameter very easily and you do not need to write your own extension methods

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

You can add attributes to the action link with an anonymous class passed as the fourth parameter:

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

The @ sign is used so that you can specify attribute names that are C # reserved keywordk (e.g. class).

+4
source share

I would probably not do it the way it would make it possible for any row. You can already do this with an action link without creating your own extension methods. Like this:

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

Personally, I prefer to use Url.Action() and write the <a /> tag myself, as I think it is more readable.

+2
source share

All Articles