簡體   English   中英

如何發送og:標題og:圖像og:說明og:從C#到Facebook的網址信息

[英]How to send the og:Title og:Image og:Description og:url info from C# to Facebook

我的頁面中有一個相似的按鈕。 單擊按鈕,我試圖在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);

}

以下是動態處理元標記的第二種方法。

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" />

現在,當我點擊Like button ,它只發送網址。 而不是發送Description/Image/Title

我正在使用鏈接“http://developers.facebook.com/tools/debug”。 它說缺少Description/Image/Title

有任何想法嗎?

您不會將元數據發送到Facebook,Facebook會在加載頁面時從頁面的HTML中檢索元數據。 嘗試使用以下工具查看您的網址:

http://developers.facebook.com/tools/debug/og/echo?q=<your URL here>

它將向您顯示Facebook看到的內容(它是您現在使用的調試工具底部的“Scraped URL”鏈接)。

如果它不包含元數據標簽,則Facebook不會看到它們,也不會將元數據添加到其Open Graph對象中。 如果是這種情況,那么您可能無法正確地將元數據添加到HTML中。

第二種方法看起來正確。 問題是,你在哪里放置代碼? 它應該在Page_Load上調用。

單擊“贊”按鈕不會發送og:xxxx信息。 您的頁面應該從一開始就已經有了這些og:xxxx元標記。

你使用MVC ASP.NET嗎?

您可以嘗試將Layout.cshtml中的元標記設置為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM