Natural Cause:Sigh. If you think its going to affect searching that's your loss.
Not really sure what you're sighing about, and yes, it certainly will affect searching. More to the point, if our clients want "B & B" in their meta tags, but don't get it, then we're not providing the service that they pay us for.
Natural Cause:Just add a literal control dynamically to the header.
Now this is more like it. I actually originally thought about going down this path, but then going one step further back than that, wondered why the control was rendering the data differently in the first place?
Thanks for everyone’s suggestions, they have been somewhat useful tackling this issue.
A while ago I needed to gain control of the <form> tag in order to govern the rendering of the action attribute. This was a relatively simple process (create a mapping in web.config, build the appropriate class to service the mapping) so I decided to employ this technique again, however this approach didn’t work.
Here’s what my code looks like now, hopefully it's useful to someone.
In the master page:
<head id="oHead" runat="server">
<title></title>
</head>
In the code-behind:
protected void SetMetaTag(string Title, string Keywords, string Description)
{
Page.Title = Title;
if ((Page.Header != null) && (Page.Header.Controls.Count > 0))
{
Page.Header.Controls.AddAt(1, new HtmlMeta("keywords", Keywords));
Page.Header.Controls.AddAt(1, new HtmlMeta("description", Description));
}
}
My HtmlMeta class:
public class HtmlMeta : System.Web.UI.HtmlControls.HtmlMeta
{
#region Constructors
public HtmlMeta(string Name, string Content)
{
base.Name = Name;
base.Content = Content;
}
#endregion
#region Constants
#endregion
#region Events
#endregion
#region Enumerations
#endregion
#region Fields
#endregion
#region Properties
#endregion
#region Methods
protected override void Render(HtmlTextWriter HtmlTextWriter)
{
HtmlTextWriter.WriteLine();
base.Render(HtmlTextWriter);
}
protected override void RenderAttributes(HtmlTextWriter HtmlTextWriter)
{
HtmlTextWriter.WriteAttribute("name", this.Name);
base.Attributes.Remove("name");
HtmlTextWriter.WriteAttribute("content", this.Content);
base.Attributes.Remove("content");
}
#endregion
#region Delegates
#endregion
}
Interstingly enough, this.Content in RenderAttributes() is not HTML encoded! The encoding must take place somewhere else. Might be time to fire up Reflector and find out where.
I would have preferred to use a straight mapping (as this would be the most elegant solution in my opinion), and I’m not sure why it wouldn’t load for me. If anyone has any idea as to how to get a tag mapping working with an HtmlMeta control, I’d be most interested!
Thanks for your time, TGIF etc!