ASP.NET Change facebook og properties from content page
10,911
Solution 1
Here is how you add the proprietary Facebook "property" attribute to a standard META tag:
HtmlMeta tag = new HtmlMeta();
tag.Attributes.Add("property", "og:title");
tag.Content = "MyTitle"; // don't HtmlEncode() string. HtmlMeta already escapes characters.
Page.Header.Controls.Add(tag);
Solution 2
if what you want is to add a property attribute in c# to HtmlControl this should be like so:
tag.Attributes.Add(KEY, VALUE);
where KEY = "property" and VALUE = "og:image"
hope this helps
Author by
user964986
Updated on June 07, 2022Comments
-
user964986 almost 2 years
I want to dynamically change in code behind facebook og properties like
< meta property="og:image" content="image_link" /> < meta property="og:title" content="title" />
How to do this?
btw. I'm adding regular meta tags like this:
HtmlMeta tag = new HtmlMeta(); tag.Name = "description"; tag.Content = message; Page.Header.Controls.Add(tag);
-
Josh Robinson over 7 yearsdoesn't work if you have code blocks <%%> in the header :(
-
Doug S over 7 years@JoshRobinson If you have <% %> code in the header, then just also hardcode the tag:
<meta property="og:title" content="<%= HttpUtility.HtmlEncode(docTitle) %>" />
. Of course, also set thedocTitle
variable in the code-behind or before that tag. -
Yannick Richard about 7 yearsthis is not working for me. Facebook crawler does not seems to read dynamically added meta tags
-
Doug S about 7 years@YannickRichard Facebook can't tell the difference between dynamically added meta tags or hardcoded meta tags. Facebook is just reading the HTML output. That means something else is wrong with your code.