views:

386

answers:

6

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 advanced!

A: 

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;
Richard
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.
Etienne
See Danrichardson's answer (http://stackoverflow.com/questions/1398821/adding-the-canonical-tag-to-my-page-for-seo-through-code-behind/1399522#1399522) for accessing a master page control from the page.
Richard
A: 

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 :)

danrichardson
A: 

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);
  }
}
Zhaph - Ben Duguid
A: 

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

Etienne
A: 

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:

in the complexType section

Michele - MMSoftware

michele
A: 

You might check out this blog post. It is an incredibly simple way to handle canonical url's in .NET. It literally takes 2 lines of code to put on each page. You basically just tell it what query string parameters you are willing to accept in your canonical url and it does the rest.

Canonical Url's in ASP.NET

damstr8