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 


qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbff250100000000a600000001000500

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";
this.Header.Controls.Add(meta);

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

// 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";
this.Header.Controls.Add(meta);




                   



Leave a comment below.

Comments

  1. Will Asrari 3/30/2006 7:11 AM
    Gravatar
    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!

    Brilliant!
  2. Ash 4/4/2006 1:06 PM
    Gravatar
    this.Header.Controls.Add(meta);

    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
    Gravatar
    Ash,

    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?

    -Ryan
  4. Ash 4/6/2006 9:05 AM
    Gravatar
    Ahh... that explains it .. am still using the beta version... thanx for the quick reply
  5. Alyssa 4/27/2006 9:02 PM
    Gravatar
    This is great .... do u have a VB version?
  6. Alyssa 4/27/2006 9:05 PM
    Gravatar
    Dim meta As New HtmlMeta
    meta.Name = "keywords"
    meta.Content = "Some words listed here"
    Page.Header.Controls.Add(meta)
  7. moNTeZIon 6/5/2006 1:48 AM
    Gravatar
    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
    Gravatar
    moNTeZIon,

    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.

    -Ryan
  9. Clinton Gallagher 6/7/2006 2:32 PM
    Gravatar
    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
    Gravatar
    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
    Gravatar
    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
    Gravatar
    great article
  13. kamal thakur 1/11/2007 8:54 AM
    Gravatar
    good article..
  14. rob 1/15/2007 11:18 PM
    Gravatar
    hey i am new at wed development and wanted to know if u can help me?
    i am changing our company site using dreamweaver 8.how do i add metatags to a .asp site

    thanks
    rob
  15. paul 3/6/2007 12:48 AM
    Gravatar
    I think this functionality is great but how would I add a conditional css link?
  16. 5/14/2007 6:10 PM
    Gravatar
    Asp.Net ile Meta Tag ekleme.. - Webmaster Forum
  17. sudhir 7/8/2007 5:54 AM
    Gravatar
    its really nice thx.
  18. sudhir 7/8/2007 5:56 AM
    Gravatar
    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
    Gravatar
    Great post thanks ... pity about the formatting of the html output though.
  20. Adam Boyar 8/13/2007 6:07 AM
    Gravatar
    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"
    metaKeywords.Attributes.Add("content",keywords)
    metaDescriptions.Name = "description"
    metaDescriptions.Attributes.Add("content",descriptions)

    me.Page.Header.Controls.AddAt(0,nl1)
    me.Page.Header.Controls.AddAt(0,metaDescriptions)
    me.Page.Header.Controls.AddAt(0,nl2)
    me.Page.Header.Controls.AddAt(0,metaKeywords)
    me.Page.Header.Controls.AddAt(0,nl3)



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

    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 system.web.ui.page and so..What might be the problem.

    Thanks

    raja
  22. Ketan Mayangar 10/12/2007 2:47 AM
    Gravatar
    Response.Redirect("'<meta http-equiv='" & Refresh & "' content='" & t & " ' URL='" & nameurl & "'></meta>")
  23. Adem AKTEPE 10/29/2007 12:22 PM
    Gravatar
    Thanks canim :)
  24. Steven Boynes 10/31/2007 1:28 PM
    Gravatar
    @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
    Gravatar
    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
    Gravatar
    thank you.
  27. Alejandro Barrada Martin 12/25/2007 5:55 PM
    Gravatar

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

    It's written in Spanish: http://abmartin.wordpress.com

  28. D 2/11/2008 7:39 PM
    Gravatar
    Thanks.
  29. DotNetKicks.com 4/13/2008 1:08 PM
    Gravatar
    You've been kicked (a good thing) - Trackback from DotNetKicks.com
  30. spam javalin 4/17/2008 6:02 AM
    Gravatar
    Ooh yeeeaaaaaah!!!!
  31. Ryan 6/11/2008 11:20 PM
    Gravatar
    Coolness, added ROBOTS meta tags to nofollow and noindex via code!
  32. Harlem 9/24/2008 8:54 PM
    Gravatar
    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
    Gravatar
    This page very use to me.TO add the meta tag in c# page it is working superbly
    Thanks
  34. Miles 10/23/2008 7:56 AM
    Gravatar
    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
    Gravatar
    Hi,
    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
    Return
    End If
    End If
    Next
    End Sub
  36. Craig Brown 1/9/2009 11:27 AM
    Gravatar
    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
    Gravatar
    Many thx
  38. Murali 5/14/2009 9:06 PM
    Gravatar
    May i know Adding Meta Tags to the Head in ASP.NET 3.5
  39. Nisse 10/1/2009 12:14 AM
    Gravatar
    Hello!
    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?

    Regards

    Nisse
  40. Joshua Gompert 12/14/2009 8:18 PM
    Gravatar
    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
    Gravatar
    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
    Gravatar
    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
    Gravatar
    [RESOLVED]Confused about where to put Meta Tags | Asp Forum
Comments have been closed on this topic.



 

News


Also see my CRM Developer blog

Connect:   @ryanfarley@mastodon.social

         

Sponsor

Sections