Monday, February 16, 2009

Adding a Canonical Link Element in ASP.NET

Google, Microsoft and Yahoo announced support for a new "canonical" link element in the header of a page that will help clean-up duplicate url's that have been indexed from your site.

The canonical link for this page, for example, would be:

<link rel="canonical" href="http://guyellisrocks.com/coding/adding-a-canonical-link-element-in-asp-net/">

To add this link tag to your header tag programatically using C# add the following to your Page_Load() function:

        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlLink link = new HtmlLink();
            link.Attributes.Add("rel", "canonical");
            link.Attributes.Add("href", "http://guyellisrocks.com/coding/adding-a-canonical-link-element-in-asp-net/");
            this.Header.Controls.Add(link);
        }

 

4 comments:

  1. Canonical url is very important. Now specially for google page rank.
    www.ommrudraksha.com

    ReplyDelete
  2. It rocks. Very good work.

    ReplyDelete
  3. Web Design MacclesfieldApril 11, 2012 at 6:25 AM

    It is good to know that Canonical HTML Link tags (and other tags, i.e. Meta and CSS Links) can be added dynamically from the C# source code.

    ReplyDelete
  4. my developer use this format in asp.net at the end of canonical, could you please tell me if this is correct ....com"></link>

    ReplyDelete