user1150440
user1150440

Reputation: 449

FaceBook like not working properly

I have used the following code to use FB Like on my website...but clicking on like posts this to FB "Jonny likes http://www.site.com" but not the actual url of the page which was liked i.e "www.site.com/reports/1".

I have placed this code in the master file...

<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

and this in the respective pages....

<div class="fb-like" data-href="http://citizen.tricedeals.com" data-send="true" data-width="450" data-show-faces="false" data-font="verdana"></div>

Upvotes: 0

Views: 443

Answers (1)

Pankaj
Pankaj

Reputation: 10115

You must implement the following Meta Tags information while doing Like press...

  1. og:title
  2. og:description
  3. og:url
  4. og:image

Code Behind

public class MetaTag
{
    public string PageURL { get; set; }
    public string TagName { get; set; }
    public string MetaTagContent { get; set; }
    public string SiteName { get; set; }

}

var fbTitleTag = new MetaTag
{
    PageURL = "/",
    MetaTagName = "og:title",
    SiteName = "Your Site Name",
    MetaTagContent = "Your Title"
};

var fbDesc = new MetaTag
{
    PageURL = "/",
    MetaTagName = "og:description",
    SiteName = "Site Name",
    MetaTagContent = "Your Description"
};

var fbUrl = new MetaTag
{
    PageURL = "/",
    MetaTagName = "og:url",
    SiteName = "Site Name",
    MetaTagContent = "URL"
};

var fbImage = new MetaTag
{
    PageURL = "/",
    MetaTagName = "og:image",
    SiteName = "Site Name",
    MetaTagContent = "Image URL"
};

System.Collections.Generic.List<MetaTag> List = new System.Collections.Generic.List<MetaTag>();
List.Add(fbTitleTag);
List.Add(fbDesc);
List.Add(fbUrl);
List.Add(fbImage);
RenderMetaTags(List, "SiteName", strRawUrl, ltMetaTags);

Here ltMetaTags is the Literal control to place in Master page. See bottom of the asnwer.

public static void RenderMetaTags(List<MetaTag> MetaTags, string sitename, string strRawURL, Literal ltlMetaHolders)
{
    //  ltlMetaHolders.Text = "";

    foreach (MetaTag oAgentMetaTag in MetaTags)
    {
        RenderMetaTagByContentName(ltlMetaHolders, oAgentMetaTag.MetaTagName, oAgentMetaTag.MetaTagContent);
    }
}

public static void RenderMetaTagByContentName(Literal ltlMetaHolder, string contentName, string content, bool isProp)
{
    var metaTagFromat = isProp ? "<meta property=\"{0}\" content=\"{1}\" />" : "<meta name=\"{0}\" content=\"{1}\" /> ";

    ltlMetaHolder.Text += string.Format(metaTagFromat, contentName, content);

}

HTML in Master Page

Following is the Literal in Head tag of Master Page

<asp:Literal ID="ltMetaTags" Mode="Transform" runat="server"></asp:Literal>

Upvotes: 1

Related Questions