How to Programmatically Add Meta Tags in ASP.net Master Pages
Changes Between Beta 1 and 2 of .Net Framework V2
In Beta 1 of the .Net Framework Version 2, you could programmatically set the meta tags of a Master page like this:
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
head.Controls.Add(meta);
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.
Resources
Tell others about
this page:
About the Author
Comments? Questions? Email Here