Saturday, 31 August 2013

Solution to check for metadata and add it if not present and modify it if it is

Solution to check for metadata and add it if not present and modify it if
it is

I am developing an C# .NET control that writes a canonical url to the
metadata of a page.
Code to be written: <link rel="canonical" href="http://www.mysite.com">
The jist of the code we are using look like this and works well.
private void setCanonicalURL(string href)
{
HtmlLink link = new HtmlLink();
link.Href = href;
link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString().ToLower(),
"canonical");
Page.Header.Controls.Add(link);
}
A new issue we are facing is that this control is being used on sites
where we have no control of the templates. At times other templates or
controls are writing the canonical metadata.
What I'd like to do is check the head to see if there was a canonical link
and if it is there modify it. If it is not there add it, but I can't find
any documentation on how to do this. I have been investigating using the
Page.Header without much luck.

No comments:

Post a Comment