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!
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
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;
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 :)
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>
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);
}
}