How to Add "nofollow" to SiteMapProvider Links in Repeater (ASP.NET)

by

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

0
1

1 Comment

anonymous by Andy on 4/3/2013
Good solution. Thanks

Add your comment

by Anonymous - Already have an account? Login now!
Your Name:

Comment:
Enter the text you see in the image below
What do you see?
Can't read the image? View a new one.
Your comment will appear after being approved.

Related Posts


If you do any sort of ASP.NET programming there usually comes a time when you need to get a websites Base URL. The following shows two examples, the first example shows how to get the Base Site Url using C#, which can be used for getting both the...  more »

Adding a CSS border to an ASP.NET Image control was a mystery to me for the longest time. While you could easily use an html image and add the runat="server" to it and then add CSS, I really wanted to use an asp:Image control along with a CSS border....  more »

Here's how you can UrlEncode the plus sign (+) in a URL querystring in ASP.NET and then retrieve the plus symbol after UrlDecoding the string. In this example, I will do a postback and redirect the Server.UrlEncoded string to another page. First we will...  more »

So below I'm going to share with you a fairly easy to use and understand ASP.NET User Control that allows you to pick a Date (with the ajaxToolkit CalenderExtender) and also select the Time of day using a drop down list. I've named the control...  more »