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

6

8

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!

Junkman answered 9/9, 2009 at 10:28 Comment(0)
J
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 information Here

Junkman answered 10/9, 2009 at 10:2 Comment(0)
H
4

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;
Housefather answered 9/9, 2009 at 11:17 Comment(2)
This is what i did, i place your link inside my MasterPage header tag. But then from my normal page your code does not work. It not picking up the canonical attribute.Junkman
See Danrichardson's answer (#1399321) for accessing a master page control from the page.Housefather
C
1

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

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

or the VB equivalent :)

Cargian answered 9/9, 2009 at 12:55 Comment(0)
I
1

Try to use: First create BasePage class 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 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 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 the complexType section

<a href="http://www.dowebpage.com">Michele - MMSoftware </a>
Instinct answered 9/6, 2010 at 12:6 Comment(0)
O
0

I have the following set up.

Create a class that inherits from System.Web.UI.Page as a "BasePage" type 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 create your .aspx pages that inherit from the base class, and then call AddHeaderLink 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);
  }
}
October answered 9/9, 2009 at 13:50 Comment(0)
E
0

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

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

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

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="forcanonical">
    <link rel="canonical" href="http://theCanonicalUrl.com" />
</asp:Content>
Exanthema answered 1/12, 2019 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.