Use meta tags (HtmlMeta objects) in ASP.NET programmatically to add meta tags that can be used by Googlebot. Google requires that webmasters have a tag that indicates that you own the site before it lets you look at its information, which makes sense. We must get meta elements working with code-behind and master pages.
In a master page setup, content pages do not have hard-coded HTML head sections. You need to add meta tags (also known as meta elements) dynamically. We do not want to have the metatags on each page, as that would just waste bandwidth. We require meta elements only on the home page.
In its object model, ASP.NET provides a handy object called HtmlMeta in the System.Web namespace. Let's look at how you can use that object in a Page_Load event. Note that I use a separate static function to deal with some of the complexity.
protected void Page_Load(object sender, EventArgs e)
{
// This code is in the code-behind for Default.aspx, called Default.aspx.cs.
// It adds a metatag dynamically when run. The metatag here is for Google
// Webmaster Tools.
AddMetaTag("verify-v1", "_NfoJpEJ/U+APc+dgVA1UV0YJqdAa+YKtP106LNZDl8=");
}
private void AddMetaTag(string name, string content)
{
// This custom function creates a new HtmlMeta object, sets its properties,
// and finally adds it to the Page.Header.Controls collection.
HtmlMeta meta = new HtmlMeta();
meta.Name = name;
meta.Content = content;
// Add the control to the HTML.
Page.Header.Controls.Add(meta);
}
<!-- Head contents come before this... --> <meta name="verify-v1" content="_NfoJpEJ/U+APc+dgVA1UV0YJqdAa+YKtP106LNZDl8=" /> </head>
HtmlMeta here makes fewer typos likely than hard-coding the HTML in various pages. Like HtmlTextWriter does for writing HTML, this approach for meta elements can reduce your risk of hard-to-find bugs. ASP.NET's markup object model is a very elegant hack that works quite well.