[ACCEPTED]-Adding the CANONICAL tag to my page for SEO through code behind?-seo

Accepted answer
Score: 16

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 1 information Here

Score: 4

Why not create your canonical element as 4 a server control:

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

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

canonical.Attributes["href"] = whatever;
Score: 1

As per Richard's answer, in your page code 2 side you will need to reference the master 1 page. Try:

((HtmlLink)this.Page.Master.FindControl("canonical")).Href = "whatever";

or the VB equivalent :)

Score: 1

Try to use: First create BasePage class 4 like this:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace MMSoftware.TheMMSoft.UI
{
    public class BasePage : System.Web.UI.Page
    {
        private string _canonical;
        // Constructor
        public BasePage()
        {
            Init += new EventHandler(BasePage_Init);
        }

        // Whenever a page that uses this base class is initialized
        // add link canonical if available
        void BasePage_Init(object sender, EventArgs e)
        {             
            if (!String.IsNullOrEmpty(Link_Canonical))
            {
                HtmlLink link = new HtmlLink();
                link.Href = Link_Canonical;
                link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString().ToLower(), "canonical");
                link.Attributes.Add(HtmlTextWriterAttribute.Type.ToString().ToLower(), "");
                link.Attributes.Add("media", "");
                Header.Controls.Add(link);
            }
        }

        /// <summary>
        /// Gets or sets the Link Canonical tag for the page
        /// </summary>
        public string Link_Canonical
        {
            get
            {
                return _canonical;
            }
            set
            {
                _canonical = value;
            }
        }                   
    }
}

Seconds create your .aspx pages 3 that inherit from the base class like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : MMSoftware.TheMMSoft.UI.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Last 2 step:

<%@ Page Title=""
         Language="C#"
         MasterPageFile="~/design/MasterPage.master"
         AutoEventWireup="true"
         CodeFile="Default.aspx.cs"
         Inherits="_Default" 
         CodeFileBaseClass="MMSoftware.TheMMSoft.UI.BasePage"
         Link_Canonical="http://yourCanonicalUrl/" 
%>

Remember to add in C:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\schemas\html\page_directives.xsd the attribute:

<xsd:attribute name="Link_Canonical" vs:nonfilterable="true" /> 

in 1 the complexType section

<a href="http://www.dowebpage.com">Michele - MMSoftware </a>
Score: 0

I have the following set up.

Create a class 5 that inherits from System.Web.UI.Page as a "BasePage" type 4 class.

Add a method to that:

public class BasePage: System.Web.UI.Page {

  // I've tended to create overloads of this that take just an href and type 
  // for example that allows me to use this to add CSS to a page dynamically
  public void AddHeaderLink(string href, 
                            string rel, 
                            string type, 
                            string media) {
    HtmlLink link = new HtmlLink();
    link.Href = href;

    // As I'm working with XHTML, I want to ensure all attributes are lowercase
    // Also, you could replace the length checks with string.IsNullOrEmpty if 
    // you prefer.
    if (0 != type.Length){
      link.Attributes.Add(HtmlTextWriterAttribute.Type.ToString().ToLower(),
                          type);
    }

    if (0 != rel.Length){
      link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString().ToLower(),
                          rel);
    }

    if (0 != media.Length){
      link.Attributes.Add("media", media);
    }

    Page.Header.Controls.Add(link);
  }
}

Then you can 3 create your .aspx pages that inherit from 2 the base class, and then call AddHeaderLink 1 on that:

public partial class MyPage : BasePage {

  protected void Page_Load(object sender, EventArgs e) {

    // Or however you're generating your canonical urls
    string cannonicalUrl = GetCannonicalUrl();

    AddHeaderLink(cannonicalUrl, "canonical", string.Empty, string.Empty);
  }
}
Score: 0

Here's what I did: In master page named 4 "Masterpage.master" head tag I 3 added the a contentPlaceHolder like this:

<asp:ContentPlaceHolder ID="forcanonical" runat="server">
</asp:ContentPlaceHolder>

Then 2 in each child aspx page page (not the code 1 behind), I added following:

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="forcanonical">
    <link rel="canonical" href="http://theCanonicalUrl.com" />
</asp:Content>

More Related questions