SEO: Adding Meta Tags to ASPX Page Programmatically

Adding page-specific meta tags, like "Description" and "Keywords" is very important in SEO optimization. Meta tags could be added directly to the head section of the web pages, such as .html or plain .aspx,but it is less than trivial in case of .aspx page referring masterpage because the head section is included in master page, so by default all pages referring to it will share the same set of meta tags.

"HtmlMeta" is a class available in "System.Web.UI.HtmlControls" namespace, which allows to add page-specific meta tags to the .aspx pages referring the same masterpage from code behind.Following is the example for how to use this class This code snippet should be included in Page_Load event of.aspx page.


HtmlHead head = (HtmlHead)Page.Header;

HtmlMeta oMeta1 = new HtmlMeta();
oMeta1.Name = "Description";
oMeta1.Content = "this is the Page description";
head.Controls.Add(oMeta1);

HtmlMeta oMeta2 = new HtmlMeta();
oMeta2.Name = "Keywords";
oMeta2.Content = "Keyword1,Keyword2....";
head.Controls.Add(oMeta2);

For example if one page deals with .NET Articals, and another page deals with Java artical and both sharing the same master page, then code snippets are like below.


For .NET Articles Page

HtmlHead head = (HtmlHead)Page.Header;

HtmlMeta oMeta1 = new  HtmlMeta();
oMeta1.Name = "Description";
oMeta1.Content = "this is the .NET Page description";
head.Controls.Add(oMeta1);

HtmlMeta oMeta2 = new  HtmlMeta();
oMeta2.Name = "Keywords";
oMeta2.Content = "C#.NET,ASP.NET,Ajax,VB.NET,OOPs,Microsoft";
head.Controls.Add(oMeta2);

For Java Articles Page

HtmlHead head = (HtmlHead)Page.Header;

HtmlMeta oMeta1 = new  HtmlMeta();
oMeta1.Name = "Description";
oMeta1.Content = "this is the Java Page description";
head.Controls.Add(oMeta1);

HtmlMeta oMeta2 = new  HtmlMeta();
oMeta2.Name = "Keywords";
oMeta2.Content = "Java,Oracle,Sun,OOPs,J2EE";
head.Controls.Add(oMeta2);

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment