.Net All About

.Net All About

How to Create Meta Tags Programmatically in ASP.NET 2.0


If you have been Developing Web, you may be familiar with the meta tags in an HTML page. The meta tags are used to provides keywords etc in an HTML page.

Now in ASP.NET 2.0, you can add these meta tags programmatically. The HtmlMeta class provides programmatic access to the HTML element on the server. The HTML element is a container for data about the rendered page, but not page content itself.

The Name property of HtmlMeta provides the property name and the Content property is used to specify the property value. The Scheme property to specify additional information to user agents on how to interpret the metadata property and the HttpEquiv property in place of the Name property when the resulting metadata property will be retrieved using HTTP.

The following code shows how to add meta tags to a page programmatically.

HtmlMeta metaTag = new HtmlMeta();
metaTag.Name = “Keywords”;
metaTag.Content = “Yogesh”;
this.Header.Controls.Add(metaTag);

Add the above created method to Load event of the Page. Now The Result can be seen by viewing dynamically cretaed source of the Page.

Similarly, you can add multiple meta tags to the header of the Page.

You can also add sechema to the Contact.

// Render:
meta = new HtmlMeta();
meta.Name = “date”;
meta.Content = DateTime.Now.ToString(“yyyy-MM-dd”);
meta.Scheme = “YYYY-MM-DD”;
this.Header.Controls.Add(meta);

July 24, 2009 - Posted by | Asp.net |

5 Comments »

  1. I tried your code with the Page_Load event of my Default.aspx
    in ASP.NET 3.5 with Visual Studio 2008.
    It didn’t work, complaining that this.Header was null

    Same for this.Page, cant do this.page.Controls.Add(meta);

    Thanks for any help!

    Comment by Peter | September 2, 2009 | Reply

    • Hello Peter,
      Can u post your code here.
      Because this code is running well in VS 2008.
      Try this if not in code.
      add runat into your head tag.

      like runat=server.

      let me know, this will help u.
      Thnx

      Comment by yrbyogi | September 3, 2009 | Reply

  2. Good one.. thanks a lot

    Comment by Karthik | May 7, 2011 | Reply

  3. you’re actually a just right webmaster. The site loading pace is amazing. It kind of feels that you’re doing any unique trick.
    Also, The contents are masterwork. you’ve done a magnificent task on this topic!

    Comment by Tonja | November 20, 2012 | Reply

  4. Heya i am for the primary time here. I found
    this board and I to find It really helpful & it helped me
    out much. I am hoping to provide something again and aid others such as you helped me.

    Comment by rosaceanaturaltreatmentx.Tumblr.com | December 24, 2012 | Reply


Leave a comment