Adding meta tag programmatically in C#

OK, I tested the answer by veggerby, and it works perfectly:

In the <header> section:

<asp:PlaceHolder id="MetaPlaceHolder" runat="server" />

Note that Visual Studio might show a warning on the PlaceHolder tag, because it is not recognised as a known element inside the header, but you can ignore this. It works.

In the C# code:

HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
MetaPlaceHolder.Controls.Add(meta);

Alternatively (since you already have code blocks using <% %> in your header section), you can tag the meta directly and retrieve only the value from server side:

<meta name="robots" content="<%=GetMetaRobotsValueFromDatabase()%>" />

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)