How to Programmatically Add Meta Tags in Master Pages

Changes Between Beta 1 and 2 of .Net Framework V2

by Lonnie Lee Best

In Beta 1 of the .Net Framework Version 2, you could programmatically set the meta tags of a Master page like this:

Header.Metadata.Add("keywords", "Keyword1, keyword2");

In Beta 2 Version 2, the Header.Metadata.Add() method was removed from the framework. So the code above generates the following error:

error CS0117: 'System.Web.UI.IPageHeader' does not contain a definition for 'Metadata'

In absence of the Metadata class, the following code will programmatically add meta tags to the header of a master page:

//Get the htmlHead your aspx page is using (from the Master page)
//Master page must include the runat server attribute in the head tag: <head runat="server">
HtmlHead head = (System.Web.UI.HtmlControls.HtmlHead)Header;

//Create a htmlMeta object
HtmlMeta meta = new HtmlMeta();

//Specify meta attributes
meta.Attributes.Add("name", "keywords");
meta.Attributes.Add("content", "keyword1, keyword2, keyword3");

// Add the meta object to the htmlhead's control collection

This is not as concise as it was in beta1, but so far, that's the quickest way I know to do it. If you know of a better way, post it to the message board.


Tell others about
this page:

facebook twitter reddit google+

About the Author

If you'd like to know more about the author of this article go to his website at (

Comments? Questions? Email Here


How to Advice .com
  1. Addiction Recovery
  2. Hospice Foundation
  3. Flat Earth Awareness
  4. Oil Painting Prints
Send us Feedback about