如何从C#向Facebook发送og:Title og:Image og:Description og:url信息

4

我在我的页面上有一个“喜欢”按钮。点击该按钮时,我正在试图在 Facebook 上发送以下标签信息...

<meta property="og:title" content="Title" />
<meta property="og:description" content="Description" />
<meta property="og:url" content="url info" />
<meta property="og:image" content="image url" />

以下是我的“点赞”按钮框架。
<iframe frameborder="0" scrolling="no" allowtransparency="true" 
  style="border: none; overflow: hidden; width: 260px; height: 35px;" 
  src="http://www.facebook.com/plugins/like.php?
  href=http://localhost:49334/WebForm1.aspx&amp;send=false&amp;
  layout=button_count&amp;width=100&amp;show_faces=false&amp;
  action=like&amp;colorscheme=light&amp;font=arial&amp;height=35">
</iframe>

以下是动态处理元标签的第一种方法。
var fbTitleTag = new MetaTag
{
    AgentPageURL = "/",
    MetaTagName = "og:title",
    UserSiteName = CurrentAgent.UserSiteName,
    MetaTagContent = Request.Cookies.Get("MasterTitle").Value
};

var fbDesc = new MetaTag
{
    AgentPageURL = "/",
    MetaTagName = "og:description",
    UserSiteName = CurrentAgent.UserSiteName,
    MetaTagContent = Request.Cookies.Get("MasterDescription").Value
};


var fbUrl = new MetaTag
{
    AgentPageURL = "/",
    MetaTagName = "og:url",
    UserSiteName = CurrentAgent.UserSiteName,
    MetaTagContent = Request.Cookies.Get("MasterURL").Value
};



var fbImage = new MetaTag
{
    AgentPageURL = "/",
    MetaTagName = "og:image",
    UserSiteName = CurrentAgent.UserSiteName,
    MetaTagContent = Request.Cookies.Get("MasterImage").Value
};

var tags = new MetaTagCollection { fbTitleTag, fbDesc, fbUrl, fbImage };

Literal ltMetaTags = null;
ltMetaTags = (Literal)this.Master.FindControl("ltMetaTags");

MetaTags(tags, "wsws", "/", ltMetaTags, true);

public static void MetaTags(MetaTagCollection MetaTags, string name, string strRawURL, Literal ltlMetaHolders, bool isProp)
{
    //  ltlMetaHolders.Text = "";

    foreach (AgentMetaTag oAgentMetaTag in agentMetaTags)
    {
        if (string.Compare(strRawURL, oAgentMetaTag.AgentPageURL, true) == 0)
        {
            if (oAgentMetaTag.MetaTagName.ToLower().Trim() != "footer" && oAgentMetaTag.MetaTagName.ToLower().Trim() != "title")
            {
                if (oAgentMetaTag.MetaTagName.ToLower().Trim() == "fbtitle")
                    oAgentMetaTag.MetaTagName = "title";

                RenderMetaTagByContentName(ltlMetaHolders, oAgentMetaTag.MetaTagName, oAgentMetaTag.MetaTagContent, isProp);
            }
        }
    }
}

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);

}

以下是第二种动态处理Meta标记的方法。
HtmlMeta tag = new HtmlMeta();
tag.Attributes.Add("property", "og:title");
tag.Content = "Title";
Page.Header.Controls.Add(tag);

HtmlMeta tag1 = new HtmlMeta();
tag1.Attributes.Add("property", "og:description");
tag1.Content = "Desc";
Page.Header.Controls.Add(tag1);

HtmlMeta tagurl = new HtmlMeta();
tagurl.Attributes.Add("property", "og:url");
tagurl.Content = "URL info";
Page.Header.Controls.Add(tagurl);

HtmlMeta tagimg = new HtmlMeta();
tagimg.Attributes.Add("property", "og:img");
tagimg.Content = "Image URL";
Page.Header.Controls.Add(tagimg);

最后,它将呈现以下元标记...
<meta property="og:title" content="Title" />
<meta property="og:description" content="Description" />
<meta property="og:url" content="url info" />
<meta property="og:image" content="image url" />

我点击“点赞按钮”时,仅会发送URL,而不会发送“描述/图片/标题”。

我使用的链接是“http://developers.facebook.com/tools/debug”。它显示缺少“描述/图片/标题”。

有任何想法吗?

3个回答

2

你是否使用MVC ASP.NET?

你可以尝试在Layout.cshtml中设置meta标签,例如:

<meta property="og:type" content="website">
<meta property="og:site_name" content="Site name">
<meta property="og:title" content="@ViewBag.OGTitle" />
<meta property="og:description" content="@ViewBag.OGDesc" />
<meta property="og:url" content="@ViewBag.OGUrl">
<meta property="og:image" content="@ViewBag.OGImage" />

然后在单独的页面MyPage.cshtml中设置标签值

@model Books.Web.Models.ItemSource
@{
    ViewBag.OGTitle = Model.Item.Title;
    ViewBag.OGDesc = Model.Item.Description;
    ViewBag.OGUrl = Request.Url.AbsoluteUri;
    ViewBag.OGImage = Request.Url.Scheme + "://" + Request.Url.Host + Url.Action("ItemCover", "Image", new { id = Model.Item.Id, height = 350 });
    Layout = "~/Views/Shared/Layout.cshtml";
}
<div>Page content here</div>

2
您不需要将元数据发送到 Facebook,Facebook 在加载页面时从页面的 HTML 中检索元数据。请尝试使用以下工具查看您的 URL:
http://developers.facebook.com/tools/debug/og/echo?q=<your URL here>

它将向您展示Facebook所看到的内容(它是您现在使用的调试工具底部的“Scraped URL”链接)。如果不包括元数据标签,则Facebook无法看到它们,也不会将元数据添加到其Open Graph对象中。如果是这种情况,那么您可能没有正确地将元数据添加到HTML中。

但是你在抓取的URL中看到元数据了吗?你正在使用Facebook调试工具,但你没有指出Facebook从你的URL中抓取的内容中看到了什么。 - Simeon Visser
但是我能够在页面查看源代码中看到元标记。我右键单击页面并检查了页面查看源代码,它显示出来了。为什么Facebook没有显示相同的内容呢? - user1425057
你是否也已经将所需的元素添加到了<html>中?它应该长这样:<html xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"> - Simeon Visser
完成这个操作后,当我点击“喜欢”按钮时,会显示确认按钮,点击确认后会弹出另一个窗口来进行点赞。我再次点击“喜欢”按钮时,会将点赞计数器重置为0,然后再次点击“喜欢”按钮会重新开始整个过程。 - user1425057
这不是一个答案,但我接受它作为最接近的答案。感谢您的帮助。 - user1425057
显示剩余4条评论

1
第二种方法看起来是正确的。问题是,你把那段代码放在哪里了?它应该在 Page_Load 中调用。
点击“赞”按钮不会发送 og:xxxx 信息。你的页面应该从一开始就有这些 og:xxxx meta 标签。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接