Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding the CANONICAL tag to my page for SEO through code behind?

I am using ASP.NET with MasterPages. Thus i cant just place this link in my pages that reference my MasterPage.

<link rel="canonical" href="http://www.erate.co.za/" />

I need to place this link in though my Page Load of each one of my pages. How would i do this through code? I am using VB.NET but C# will also help me in the right direction.

This is how i did it for my DESCRIPTION tag in my code behind.

    Dim tag As HtmlMeta = New HtmlMeta()
    tag.Name = "description"
    tag.Content = "Find or rate any company in South Africa for FREE and rate them"
    Header.Controls.Add(tag)

Thanks in advance!

like image 693
Etienne Avatar asked Sep 09 '09 10:09

Etienne


2 Answers

This is what i had to do..................

    Dim seoTag As HtmlLink = New HtmlLink()
    seoTag.Attributes.Add("rel", "canonical")
    seoTag.Href = "http://www.erate.co.za/"
    Header.Controls.Add(seoTag)

More information Here

like image 90
Etienne Avatar answered Oct 24 '22 17:10

Etienne


Why not create your canonical element as a server control:

<link rel="canonical" href="" runat="server" id="canonical"/>

Then manipulate the canonical object in your page (or master page) class. Generic tags are treated as instances of HtmlGenericControl which allows one to set arbitrary attributes:

canonical.Attributes["href"] = whatever;
like image 38
Richard Avatar answered Oct 24 '22 17:10

Richard