Dynamically Change Meta Tags in ASP.net
Well, I had a project that stumped me for a moment. I understand how to dynamically inject meta tags through System.Web.UI.HtmlControls.HtmlHead. But, what if you already have Meta Tags set? I really didn’t have a clue. So, I had to take the dreaded trip to MSDN to browse object definitions and came up with the quickest thing that works. One day I will refactor it, but this works for now.
The application I had to update uses Base Pages and includes code to pull a default title, description, and keywords from a configuration file. My task was to overwrite the default meta values if the values are present in the record set retrieved from the database. My solution involves a simple method placed in the Base Page class that searches the head control collection to find and replace the appropriate meta tags.
public static void WriteMetaTag(System.Web.UI.HtmlControls.HtmlHead pHead, string pName, string pContent)
{
//Remove the MetaTag if it is already set
//Loop through the controls in the head
for (int i = pHead.Controls.Count - 1; i >= 0; i--)
{
//If the current head control is an HtmlMeta object, inspect it
if (pHead.Controls[i] is HtmlMeta)
{
//Cast the current control to an HtmlMeta
HtmlMeta thisMetaTag = (HtmlMeta)pHead.Controls[i];
//If the name matches the name passed into the method remove it
if (thisMetaTag.Name == pName)
{
pHead.Controls.RemoveAt(i);
}
}
}
//Declare a new meta control
HtmlMeta metaTag = new HtmlMeta();
//Set the name of the meta tag
metaTag.Name = pName;
//Set the content of the meta tag
metaTag.Content = pContent;
//Add the meta tag to the control collection
pHead.Controls.Add(metaTag);
}
If meta is set in database, I call this method for each value that I have to override. I just feed it an HtmlHead, meta name, and meta content (from the db) and it does the rest. It’s not rocket science and not elegant with all the looping and if’ing, but you can see how it works by reading the comments. If you have something better, please let me know. If you want to talk about it, leave me a comment.


hey,Great blogging dude! i am Tired of using RSS feeds and do you use twitter?so i can follow you there:D.
PS:Do you thought putting video to the web site to keep the readers more entertained?I think it works.Yours, Joi Durough
Thanks Joi. I don’t regularly use Twitter (@charleslbryant), but I do tweet new blog posts.
I thought about video, but its too much work for me.
Hello. I have a problem in function firs argument.
How can you use this function in your code.
I called like this but nothing changed.
WriteMetaTag((HtmlHead)Page.Header, “keywords”, dtCategory.Rows[0]["keywords"].ToString());
I just want to change or remove old metatags.
The best advice I can give is to step through the method in debugger to see if it is finding your meta tags. I would have to have more of your code to give a better solutions. Hope it works out for you, but if it doesn’t send me some of your code and we can work through it.