RSS 2.0 Feed
RSS 2.0

Atom 1.0 Feed
Atom 1.0

  Adding Meta Tags to the Head in ASP.NET 2.0 

After my last post on adding items to the page head in ASP.NET 2.0, Karthik Nataraaj asked if there was a way to add meta tags as well. You are in luck Karthik. The HtmlMeta class is provided for just that. You can easily create a HtmlMeta object and add it to the Controls collection in the HtmlHead class exposed via Page.Header. Here's a few samples:

// Render: <meta name="keywords" content="Some words listed here" />
HtmlMeta meta = new HtmlMeta();
meta.Name = "keywords";
meta.Content = "Some words listed here";

// Render: <meta name="robots" content="noindex" />
meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex";

// Render: <meta name="date" content="2006-03-25" scheme="YYYY-MM-DD" />
meta = new HtmlMeta();
meta.Name = "date";
meta.Content = DateTime.Now.ToString("yyyy-MM-dd");
meta.Scheme = "YYYY-MM-DD";


Leave a comment below.


  1. Will Asrari 3/30/2006 7:11 AM
    This would be a perfect addition for a content management system. For each page the user creates the keywords and description can be stored in a database. This way it never has to be changed programmatically!

  2. Ash 4/4/2006 1:06 PM

    I recd. an error at the above line:
    'System.Web.UI.IPageHeader' does not contain a definition for 'Control'

    Please let me know where I'm going wrong.
  3. Ryan Farley 4/4/2006 1:57 PM

    Make sure your page inherits from System.Web.UI.Page and that you're using the .NET 2.0 release (not a beta - things were a bit different then). Does that check out?

  4. Ash 4/6/2006 9:05 AM
    Ahh... that explains it .. am still using the beta version... thanx for the quick reply
  5. Alyssa 4/27/2006 9:02 PM
    This is great .... do u have a VB version?
  6. Alyssa 4/27/2006 9:05 PM
    Dim meta As New HtmlMeta
    meta.Name = "keywords"
    meta.Content = "Some words listed here"
  7. moNTeZIon 6/5/2006 1:48 AM
    In which event have to code this?
    In Page_Load doesn't works. No error, but i can't see de MetaTags when i see the page source code in the explorer.
    Thanks :-)
  8. Ryan Farley 6/5/2006 9:35 AM

    I can put this code about anywhere. Literally anywhere. I've done it in the Page_Load, also in the click event behind a button (so on the postback it adds some style attributes & other header stuff).

    In any of those scenarios, I can look at the source and see the new header values.

  9. Clinton Gallagher 6/7/2006 2:32 PM
    Writing meta tags and style declarations into the head is good. Not being able to control source formatting is bad.

    Has anybody actually learned how to use these new classes so each meta tag for example or linked css/javascript file can be on its own line? That would I presume emitting a CRLF would it not?
  10. Rookie 6/13/2006 10:29 PM
    Doing it this method is nice and simple. but it does lack some controlability (please refer to the above post). Why would you want to control it? well if you use themes with styles sheets ASP.NET will automatically add the linking tag for the style sheet and it will do this before it adds the meta tags and this is bad for search engines since you want your meta tags turn up as early as possible. Anyone know how to improve on this?
  11. malmojoe 9/4/2006 3:57 AM
    If you want to add these meta tags at the begining of the head tag, or directly after the 'Content-Type' meta tag, try using AddAt(0,myMeta) intsead of add, where 0 is the Page.Header.Controls array index.
  12. ismail tutumler 1/9/2007 11:37 PM
    great article
  13. kamal thakur 1/11/2007 8:54 AM
    good article..
  14. rob 1/15/2007 11:18 PM
    hey i am new at wed development and wanted to know if u can help me?
    i am changing our company site using dreamweaver do i add metatags to a .asp site

  15. paul 3/6/2007 12:48 AM
    I think this functionality is great but how would I add a conditional css link?
  16. 5/14/2007 6:10 PM
    Asp.Net ile Meta Tag ekleme.. - Webmaster Forum
  17. sudhir 7/8/2007 5:54 AM
    its really nice thx.
  18. sudhir 7/8/2007 5:56 AM
    do u know how to restrict a class to create maximum of five instances only using c#?
  19. JasonM 7/21/2007 4:55 PM
    Great post thanks ... pity about the formatting of the html output though.
  20. Adam Boyar 8/13/2007 6:07 AM
    Actually, if you want to add the Carriage return as requested just use this routine

    'Load Meta Data
    Protected Sub LoadMeta(title as String, descriptions as String, keywords as String)
    dim metaDescriptions, metaKeywords as new UI.HtmlControls.HtmlMeta
    me.Page.Header.Title = title

    dim nl1,nl2,nl3 as New Web.UI.WebControls.Literal
    nl1.Text = environment.NewLine
    nl2.Text = environment.NewLine
    nl3.Text = environment.NewLine

    metaKeywords.Name = "keywords"
    metaDescriptions.Name = "description"


    End Sub
  21. raja 8/13/2007 8:43 AM
    HtmlMeta meta = new HtmlMeta();
    meta.Name = "keywords";
    meta.Content = "Some words listed here";

    I have added the above code in my page but i got Object reference not set to an instance of an object. error but i am and so..What might be the problem.


  22. Ketan Mayangar 10/12/2007 2:47 AM
    Response.Redirect("'<meta http-equiv='" & Refresh & "' content='" & t & " ' URL='" & nameurl & "'></meta>")
  23. Adem AKTEPE 10/29/2007 12:22 PM
    Thanks canim :)
  24. Steven Boynes 10/31/2007 1:28 PM
    @Adam Boyar - thanks for the vb tip. I had to implement similar in C# and did the following:

    LiteralControl newln0 = new LiteralControl(Environment.NewLine);
    Header.Controls.AddAt(1, newln0);
    Header.Controls.AddAt(2, meta_description);

    I'm omitting the actual meta_description declaration in my snippet, but basically this gets me a newline in the source view just before the meta description tag/control.
  25. Ando Poore 11/13/2007 6:04 AM
    I'm using C#, and I'm trying to pull off the Response-Redirect with the meta tag inside (as I'm trying to get it to 'redirect' to a file to download)inside a button's click event, but I keep getting HTTP400 errors when I try to click the button. Any further pointers?
  26. çanakkale 12/20/2007 10:36 AM
    thank you.
  27. Alejandro Barrada Martin 12/25/2007 5:55 PM

    Hello, I write a post for all of those who want to write headers meta tags trought code.

    It's written in Spanish:

  28. D 2/11/2008 7:39 PM
  29. 4/13/2008 1:08 PM
    You've been kicked (a good thing) - Trackback from
  30. spam javalin 4/17/2008 6:02 AM
    Ooh yeeeaaaaaah!!!!
  31. Ryan 6/11/2008 11:20 PM
    Coolness, added ROBOTS meta tags to nofollow and noindex via code!
  32. Harlem 9/24/2008 8:54 PM
    How to add the all code at the same time ?

    To render at the same time ?
    // <meta name="keywords" content="Some words listed here" />
    // <meta name="robots" content="noindex" />
    // <meta name="description" content="words, a,b,c,d,...." />

    I can't let those exist , please give me directions, tkx!!

  33. sasi 9/29/2008 9:44 PM
    This page very use to me.TO add the meta tag in c# page it is working superbly
  34. Miles 10/23/2008 7:56 AM
    I'd like to check for meta values before I set them as not to overwrite. Is this possible?
  35. mb 11/29/2008 5:18 AM
    This is useful...

    But because I used <%=Something %> code block in header, I getting error like
    "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). "

    So, I am searched to another method.

    I found one... But, this can set new value to already exist tags.
    But I don't sure if this is a good method because the function each time searching for the wanted meta tag.


    Public Shared Sub MetaTag(ByVal thisPage As Page, ByVal name As String, ByVal content As String)

    Dim meta As New HtmlMeta
    ' Loop through all the controls in the Header
    For Each ctrl As Control In thisPage.Header.Controls
    ' Check if the control is a Meta Tag
    If TypeOf ctrl Is HtmlMeta Then
    ' Get the Meta Tag and check if the name is the same
    meta = DirectCast(ctrl, HtmlMeta)
    If name.Equals(meta.Name, StringComparison.InvariantCultureIgnoreCase) Then
    ' Since the Meta Tag already exists, simply update and exit
    meta.Content = content
    End If
    End If
    End Sub
  36. Craig Brown 1/9/2009 11:27 AM
    A good place to put keyword and description meta information would be in your web.sitemap! I use the description key for each page's meta description and add a new key for each node in the sitemap called "keywords" and assign the keywords for each page to this value. I then use the method Ryan has outline so well above in my masterpage to grab this information and generate the page's tags on the fly.
  37. malk 5/12/2009 11:58 PM
    Many thx
  38. Murali 5/14/2009 9:06 PM
    May i know Adding Meta Tags to the Head in ASP.NET 3.5
  39. Nisse 10/1/2009 12:14 AM
    Great article.
    I am trying to implement this suggestion (with carriage returns):

    'Load Meta Data
    Protected Sub LoadMeta(title as String, descriptions as String, keywords as String)

    that Adam Boyar wrote. And it works fine if I place that Subroutine code inside the contents codebehind file. But I would like to place this code in a separate class in a class file. And call that sub from the contents codebehind file.
    But I only get "Object reference not set to an instance of an object" error then. Any ideas why?


  40. Joshua Gompert 12/14/2009 8:18 PM
    Great article, I have already implemented it in my code as I write this. Thanks a lot.
  41. iorien 2/17/2010 3:33 AM
    Hello ryan

    What should we do if we have two sitemap providers in our web.config? How can I change provider to make the meta title and meta description work with the apropiate provider?

    Thank you very much.
  42. VincentIT 6/17/2010 11:53 PM
    Very Good Article. But form the master page can we change or edit the mata tags of other pages of the site.
  43. 9/9/2014 3:08 PM
    [RESOLVED]Confused about where to put Meta Tags | Asp Forum
Comments have been closed on this topic.



Also see my CRM Developer blog