How to add image url in og tag

WebForm_Developer 1 Reputation point
2022-04-09T14:17:39.023+00:00

Hi,
I have a List of Products with Title, Photo and some other contents.
When I click on Details link it shows me details of selected Product with Image.

I want to add this dynamic loaded image in meta tag for "og:image" - og tags

I tried it but I unable to set image URL in meta tag.

Photo field is string type i am storing URL here.

asp.net web forms - C#
Thanks

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
1,490 questions
{count} votes

4 answers

Sort by: Most helpful
  1. WebForm_Developer 1 Reputation point
    2022-04-09T20:35:03.653+00:00

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

    I wrote above code in Page_load()

    thanks


  2. WebForm_Developer 1 Reputation point
    2022-04-11T05:42:59.407+00:00

    HI
    thanks for reply.
    No I am not getting any error message. But og:image is not showing on whatsapp. I tried below tests.

    Tests:
    1] Response.Write("<meta property='og:image' content=" + imgAdd + " itemprop='image'>");

    2] Response.Write("<meta property='og:image:secure_url' content=" + imgAdd + " itemprop='image'>");

    3] Response.Write("<meta property='og:image' content=" + imgAdd + " itemprop='image'>");

    imgAdd is the variable where I stored sent Image url.

    I removed my previous code which I mentioned above in Page_Load

    0 comments No comments

  3. Lan Huang-MSFT 15,296 Reputation points Microsoft Vendor
    2022-04-11T06:21:17.17+00:00

    Hi @WebForm_Developer ,
    I tried your code, the problem is the missing quotes.
    Response.Write("<meta property='og:image' content=" + imgAdd + " itemprop='image'>");
    output:
    191726-image.png

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

    output:
    191714-image.png
    You can see the difference by looking at the output above.You can see the difference by looking at the output above, so you need to put single quotes around the content.**content='" + imgAdd + "'**.
    Response.Write("<meta property='og:image' content='" + imgAdd + "' itemprop='image'/>");
    output
    191727-image.png
    If you have extra questions about this answer, please click 191706-image.png.
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. WebForm_Developer 1 Reputation point
    2022-04-11T06:33:44.053+00:00

    No. Still not working....

    Updated code:

    Response.Write("<meta property='og:image' content='" + imgAdd + "' itemprop='image'>");
    Response.Write("<meta property='og:image:secure_url' content='" + imgAdd + "' itemprop='image'>");

    It's working very fine if I paste the URL on facebook or send SMS. Only problem while sending with whatsapp

    0 comments No comments