How to Add "nofollow" to SiteMapProvider Links in Repeater (ASP.NET)
by
Doug
December 11, 2010
Add rel="nofollow" tags to ASP.NET SiteMap links that are bound to a Repeater control.
Here's how you can add "nofollow" tags to links generated by a Sitemap file that is bound to an ASP.NET Repeater control using a SiteMapDataSource.
I'm currently using this technique for the footer links of GotKnowHow.com, so if you View Source of the html, you will notice that the Contact, Privacy and Terms links all have: rel="nofollow". This is typically considered good practice in terms of SEO. Since those links are on every page, and are links to non-essential pages in terms of SEO, I don't need them to be ranked or want those links to take away from other more important internal links on the site.
Here's what I did to add nofollow to SiteMapDataSource links within a Repeater control:
1. I added my own custom tag called IsNoFollow="true" to nodes in the ASP.NET .sitemap file where I wanted the nofollow tag appended to links, such as for the Contact Us, Privacy Policy and Terms SiteMap footer links.
siteMapNode url="~/info/privacy.aspx" title="Privacy" description="" IsNoFollow="true"
2. I then added an ItemDataBound event to the Repeater, that looks for "IsNoFollow" within the SiteMapNode for each bound node, so that the rel="nofollow" attribute is added to the tagged HyperLinks within the Repeater control.
Here's the code for the ItemDataBound event:
protected void FooterRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SiteMapNode node = e.Item.DataItem as SiteMapNode;
string noFollow = node["IsNoFollow"];
if (!string.IsNullOrEmpty(noFollow) && noFollow == "true")
{
HyperLink hlnkFooter = (HyperLink)e.Item.FindControl("FooterHyperLink");
hlnkFooter.Attributes.Add("rel", "nofollow");
}
}
}
To see the full ASP.NET / C# example code, download the .zip file below.
FILES:
SiteMap - NoFollow Links in Repeater Example.zip