ASP.NET SEO and the Canonical Tag

by agrace 21. February 2009 08:03

Canonical Tag Recently, Google, Microsoft and Yahoo announced support for a new canonical tag in an effort to combat duplicate urls (read duplicate content) on websites. Quite simply, you can add an HTML <link> tag to the <head> section of your page to indicate the preferred, or canonical, version of the page url.

If you have something like this:




You can have the search spider interpret it as this:


It works like a 301 redirect and is just a hint to the search spider. In other words, the other search engines are free to recognize it or not. It's mind-boggling the amount of work developers have had to do to get around this problem up to now, and it was this simple to fix at the end of the day?

To implement this for your ASP.NET products page, with its GridView of pageable, sortable widgets, you could do the following:

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class Products : Page
    protected void Page_Load(object sender, EventArgs e)
        HtmlLink canonicalTag = new HtmlLink();
        canonicalTag.Href = "";
        canonicalTag.Attributes["rel"] = "canonical";


ASP.NET renders the following: 

    <title>Products Page</title>
    <link href="" rel="canonical" />


There is only one problem with this if you are using a XHTML doctype declaration; per the W3C recommendation, a closing slash in the <link> tag is illegal. The correct format is:

    <link href="" rel="canonical">


So where does this leave us? For me, relaxing the doctype to anything less than XHTML transitional is not an option. Does this mean we have to use an HtmlTextWriter to customize the output for this particular tag or is there some easier way? Has anyone got a suggestion or will we have to wait for a fix?

kick it on

Tags: ,


Comments (10) -

Andrew Nurse
Andrew Nurse Canada
2/27/2009 4:44:38 AM #

Cool stuff!  I'm not sure about the XHTML compliance issue though.  The link you provided is to the HTML 4.01 spec, but some quick tests on the W3C Validator ( seemed to indicate that XHTML 1.0 (Transitional/Strict) and XHTML 1.1 both support an empty "<link />" tag.

Andrew Nurse
Andrew Nurse Canada
2/27/2009 4:45:41 AM #

The link in the above post should be

Can't edit the comment :S

agrace United States
2/27/2009 4:58:18 AM #

Thanks for pointing that out Andrew, good news indeed Smile Like, we don't have enough to contend with, with IE8 coming out any day now?!
Anthony Smile

Chris United States
7/30/2009 11:24:49 AM #

What would be the next step in programmatically adding the tag to each page/post in

eiretics Netherlands
8/27/2009 10:48:54 PM #

The <link> element should be self closing in xHTML, ie. <link href=""; rel="canonical" />

In HTML 4 it is not self closing.

seo indore
seo indore United States
7/5/2010 11:19:12 AM #

I am glad to see that some webmasters are still there who share there valuable suggestions to the visitors.

Thanks for the resource.

SEO India
SEO India United States
8/31/2011 5:54:19 AM #

Canonical issue is a a major issue in the site,Thanks For this good information for Removing the canonical issue

Web Design Macclesfield
Web Design Macclesfield United Kingdom
4/11/2012 3:32:29 AM #

You can also use HtmlMeta to dynamically add Meta Tags... adding Meta Tags and Links can be done selectively, i.e. for JavaScript or CSS includes that you only need on certain pages.

Very useful.


karen United States
10/16/2012 4:02:27 AM #

My developer place the canonical as such"></link>
Is this correct, if not please give me correct format for


Web Developer
Web Developer United States
5/13/2013 10:30:23 PM #

You can use dynamic HtmlMeta add meta tags ... adding metadata and links may be selective or JavaScript or CSS means that it is only necessary for some sites.

Pingbacks and trackbacks (7)+

Add comment

  Country flag
  • Comment
  • Preview