Welcome

Welcome to CMS n Web. Learn & Study completely Free Articles\Training about Web Designing\Development and CMS.

Search This Web

Monday, September 16, 2013

SEO optimization: add meta tags to ASP.NET page programmatically

Problem Definition

Adding page-specific meta tags, like "description" and "keywords" is quintessentially important in a context of SEO optimization. Meta tags can be added directly to the head section of the regular web pages, such as .html or .aspx (i.e. ASP.NET pages), but it is not a trivial task in case of .aspx page built on the top of master page for the following reason: head section appears just in master page, so by default all derivative pages referring to it will share the same set of meta tags.

Solution

Simple coding technique demonstrated in the following snippets (see Listing 1) allows adding various page-specific meta tags to.aspx pages referring to the same master page:
Listing 1 
HtmlMeta _metaD = new HtmlMeta();
_metaD.Name = "Description";
_metaD.Content = "WebTV, Internet Broadcasting, Free Video Online, Free Online Music, Best YouTube Videos";
 
HtmlMeta _metaK = new HtmlMeta();
_metaK.Name = "Keywords";
_metaK.Content = "Best YouTube Videos, Free Music Video Online, Pop Rock Channel, Smooth Jazz, Hip Hop, Rap, New Wave, 80s, 90s, Disco, WebTV, Internet Broadcasting";
 
((Control)Header).Controls.Add(_metaD);
((Control)Header).Controls.Add(_metaK);
This code snippet should be included in Page_Load procedure of .aspx page; don’t forget to add the reference to System.Web.UI.HtmlControls on the page level.

By 11 Sep 2013 on code project