dynamically create facebook share metatags [og:] in asp.net

11,437

Solution 1

All your meta tags in the code behind are missing the part where you set the property attribute. Something like

HtmlMeta tag = new HtmlMeta();
tag.Attributes.Add("property", "og:title");
tag.Content = "Title";
Page.Header.Controls.Add(tag);

See the following thread, How to send the og:Title og:Image og:Description og:url info from C# to Facebook

Solution 2

For Google+, schema.org microdata is preferred over Open Graph tags. You can decorate any HTML tags with schema.org microdata and can use the Google+ snippet tool to generate example content. In your case, you could just add an itemscope to the HTML tag such as:

<html itemscope itemtype="http://schema.org/Article">

<head>
  <meta itemprop="name" content="Blog title">
  <meta itemprop="description" content="A blog post description">
  <meta itemprop="image" content="http://example.com/test.jpg">
</head>
<body>
  ...

In your case, all you need to do to improve the result for Google+ is add the itemprop tags to your code, for example:

case "og:site_name":
  tag.Name = str;
  tag.Attributes.Add("itemprop", "name");
  tag.Content = "http://www.mysite.com";
  Page.Header.Controls.Add(tag);
  break;
Share:
11,437
patel.milanb
Author by

patel.milanb

Updated on June 05, 2022

Comments

  • patel.milanb
    patel.milanb almost 2 years

    I am trying to create meta tags in code behind for my website which has a blog section. so each blog will have their own title, image. hence created in code behind. just wanted to make sure if this is right and Facebook/Google+ will recognize it.

    CODE BEHIND

    private void AddMetaTagsForPage(object tempObject)
          {
                string[] metaTags = {"og:title", "og:site_name", "og:type", "og:url", "og:image"};
    
                foreach(string str in metaTags)
                {
                      HtmlMeta tag = new HtmlMeta();
    
                      switch(str)
                      {
                            case "og:title":
                                  tag.Name = str;
                                  tag.Content = tempObject.ChallengeTitle;
                                  Page.Header.Controls.Add(tag);
                                  break;
                            case "og:site_name":
                                  tag.Name = str;
                                  tag.Content = "http://www.mysite.com";
                                  Page.Header.Controls.Add(tag);
                                  break;
                            case "og:type":
                                  tag.Name = str;
                                  tag.Content = "blog";
                                  Page.Header.Controls.Add(tag);
                                  break;
                            case "og:url":
                                  tag.Name = str;
                                  tag.Content = HttpContext.Current.Request.Url.AbsoluteUri;
                                  Page.Header.Controls.Add(tag);
                                  break;
                            case "og:image":
                                  tag.Name = str;
                                  tag.Content = String.Format("http://static.mountainwarehouse.com" + tempObject.ChallengeImageURL);
                                  Page.Header.Controls.Add(tag);
                                  break;
                      }
                }
          }
    

    GENERATED HTML

        <meta content="No appointment necessary. We hear you coming." name="og:title">
        <meta content="http://www.mysite.com" name="og:site_name">
        <meta content="blog" name="og:type">
        <meta content="http://dev.mysite.com/entries/viewentry.aspx?entryId=34"
        name="og:url">
        <meta content="http://static.mysite.com/Images/2a7803a2-e8f1-424c-9f53-3ddc1fe33c3e.jpg" name="og:image">
    

    on facebook and google+ it says that you must have meta tags in this format.

    <meta content="No appointment necessary. We hear you coming." property="og:title">
    

    instead of property keywords i have name. I dont know how to create that in asp.net. is it valid for facebook?

    please help