ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Display Facebook Open Graph Tags from code behind using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="firoz1986">firoz1986</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>There are two approach you can add the meta tag in the header.</p> <p><span style="text-decoration: underline;"><strong>Approach 1</strong></span></p> <p><strong>Note-</strong></p> <p><strong>&nbsp;</strong>You need to remove runat = "server" from head tag</p> <p>Using property class.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: html">&lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;%foreach (MetaTag meta in this.Meta_Tags) { %&gt; &lt;meta property=&#34;&lt;%=meta.Property %&gt;&#34; content=&#34;&lt;%=meta.Content %&gt;&#34; /&gt; &lt;%} %&gt; &lt;/head&gt;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: html">&lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;%For Each meta As MetaTag In Me.Meta_Tags %&gt; &lt;meta property=&#34;&lt;%=meta.Property %&gt;&#34; content=&#34;&lt;%=meta.Content %&gt;&#34; /&gt; &lt;%Next%&gt; &lt;/head&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Collections.Generic;</pre> <p><strong><span style="text-decoration: underline;">Code </span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected List&lt;MetaTag&gt; Meta_Tags { get; set; } protected void Page_Load(object sender, EventArgs e) { this.Meta_Tags = new List&lt;MetaTag&gt;(); this.Meta_Tags.Add(new MetaTag { Property = &#34;og:title&#34;, Content = &#34;How to show OG tags?&#34; }); this.Meta_Tags.Add(new MetaTag { Property = &#34;og:image&#34;, Content = &#34;123.jpg&#34; }); this.Meta_Tags.Add(new MetaTag { Property = &#34;og:site_name&#34;, Content = &#34;123.com&#34; }); this.Meta_Tags.Add(new MetaTag { Property = &#34;og:description&#34;, Content = &#34;sqsqs fefe fefef&#34; }); } public class MetaTag { /// &lt;summary&gt; /// Gets or Sets Property. /// &lt;/summary&gt; public string Property { get; set; } /// &lt;summary&gt; /// Gets or Sets Content. /// &lt;/summary&gt; public string Content { get; set; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Property Meta_Tags As List(Of MetaTag) Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Me.Meta_Tags = New List(Of MetaTag)() Me.Meta_Tags.Add(New MetaTag With { .[Property] = &#34;og:title&#34;, .Content = &#34;How to show OG tags?&#34; }) Me.Meta_Tags.Add(New MetaTag With { .[Property] = &#34;og:image&#34;, .Content = &#34;123.jpg&#34; }) Me.Meta_Tags.Add(New MetaTag With { .[Property] = &#34;og:site_name&#34;, .Content = &#34;123.com&#34; }) Me.Meta_Tags.Add(New MetaTag With { .[Property] = &#34;og:description&#34;, .Content = &#34;sqsqs fefe fefef&#34; }) End Sub Public Class MetaTag Public Property [Property] As String Public Property Content As String End Class</pre> <p><strong><span style="text-decoration: underline;">Approach 2</span></strong></p> <p>Using HtmlMeta class.</p> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Web.UI.HtmlControls;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[2] { new DataColumn(&#34;Property&#34;, typeof(string)), new DataColumn(&#34;Content&#34;, typeof(string))}); dt.Rows.Add(&#34;og:title&#34;, &#34;How to show OG tags?&#34;); dt.Rows.Add(&#34;og:image&#34;, &#34;123.jpg&#34;); dt.Rows.Add(&#34;og:site_name&#34;, &#34;123.com&#34;); dt.Rows.Add(&#34;og:description&#34;, &#34;sqsqs fefe fefef&#34;); foreach (DataRow row in dt.Rows) { HtmlMeta htmlMeta = new HtmlMeta(); htmlMeta.Attributes.Add(&#34;property&#34;, row[&#34;Property&#34;].ToString()); htmlMeta.Content = row[&#34;Content&#34;].ToString(); this.Page.Header.Controls.Add(htmlMeta); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(1) {New DataColumn(&#34;Property&#34;, GetType(String)), New DataColumn(&#34;Content&#34;, GetType(String))}) dt.Rows.Add(&#34;og:title&#34;, &#34;How to show OG tags?&#34;) dt.Rows.Add(&#34;og:image&#34;, &#34;123.jpg&#34;) dt.Rows.Add(&#34;og:site_name&#34;, &#34;123.com&#34;) dt.Rows.Add(&#34;og:description&#34;, &#34;sqsqs fefe fefef&#34;) For Each row As DataRow In dt.Rows Dim htmlMeta As HtmlMeta = New HtmlMeta() htmlMeta.Attributes.Add(&#34;property&#34;, row(&#34;Property&#34;).ToString()) htmlMeta.Content = row(&#34;Content&#34;).ToString() Me.Page.Header.Controls.Add(htmlMeta) Next End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="https://i.imgur.com/WzKMsUk.jpg" alt="" width="468" height="310" /></strong></span></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/416375/Display-Facebook-Open-Graph-Tags-from-code-behind-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/416375/Display-Facebook-Open-Graph-Tags-from-code-behind-using-C-and-VBNet-in-ASPNet/Thu, 31 May 2018 07:13:34 GMT