简体   繁体   English

如何动态设置Facebook共享的图像

[英]How to set the Image for facebook share dynamically

I set all my properties for the facebook share in the c# codebehind. 我在后面的C#代码中为Facebook共享设置了所有属性。 However I am not having any luck with the Image, and I cannot find any solutions. 但是,我对Image没有任何运气,也找不到任何解决方案。

How I add the site name: 我如何添加站点名称:

HtmlMeta siteNameProp = new HtmlMeta();
siteNameProp.Attributes.Add("property", "og:site_name");
siteNameProp.Content = "MyName";
Page.Header.Controls.Add(siteNameProp);

How I try to add the Image: 我如何尝试添加图像:

HtmlMeta imageProp = new HtmlMeta();
imageProp.Attributes.Add("property", "og:image");
imageProp.Content = "/Content/Images/FB-share.png";
Page.Header.Controls.Add(imageProp);

My Image size is 1200x630 (I read on SO that this size is recommended). 我的图片尺寸为1200x630(建议继续阅读,建议使用此尺寸)。 And the Path to my Image is /Content/Images/FB-share.png. 我图像的路径是/Content/Images/FB-share.png。

No image is showing when I press share. 按共享时没有图像显示。 What can I do? 我能做什么? is the path wrong? 路径错了吗? Not that good with paths in c#. 使用C#中的路径效果不佳。

Have you tried using https://developers.facebook.com/tools/debug ? 您是否尝试过使用https://developers.facebook.com/tools/debug

There is an Open Graph properties section to check if you set the image value correctly as og:image 有一个“打开图属性”部分,用于检查您是否将图像值正确设置为og:image

You should also start using a lower resolution size than 1200 x 630 pixels: https://developers.facebook.com/docs/sharing/best-practices#images 您还应该开始使用低于1200 x 630像素的分辨率: https : //developers.facebook.com/docs/sharing/best-practices#images

[EDIT] [编辑]

There were some cases that Facebook randomly chooses a picture that has a lowest resolution size, so what I did is that I explicitly set the og:image value to the meta: 在某些情况下,Facebook会随机选择分辨率最低的图片,所以我要做的是将og:image值明确设置为meta:

<meta id="featuredimage" runat="server" name="og:image" />

Then set the content value to my code-behind: 然后将内容值设置为我的代码背后:

featuredimage.Attributes.Add("content", "image url here");

Happy coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM