如何将og:标题og:图片og:描述og:url信息从C#发送到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&send=false&
layout=button_count&width=100&show_faces=false&
action=like&colorscheme=light&font=arial&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" />
现在,当我单击“赞”按钮时,它仅发送url.而不发送描述/图像/标题.
我正在使用链接“ http://developers.facebook.com/tools/debug”.它说描述/图像/标题丢失.
有任何想法吗?
解决方法:
您无需将元数据发送到Facebook,Facebook在加载页面时会从页面的HTML检索元数据.尝试使用以下工具查看您的URL:
http://developers.facebook.com/tools/debug/og/echo?q=<your URL here>
它将向您显示Facebook看到的内容(这是您现在使用的调试工具底部的“抓取的URL”链接).
如果它不包含元数据标签,那么Facebook将看不到它们,也不会将元数据添加到其“ Open Graph”对象中.如果是这种情况,那么您可能没有将元数据正确添加到HTML.
标签:c-4-0,facebook-like,meta-tags,asp-net,c 来源: https://codeday.me/bug/20191101/1982078.html