Why does the runat attribute in head-tag change the way ASP.NET handles meta tags?

Example 1:

<head>
  <meta http-equiv="description" content="<%= Foo %>"/>
</head>

      

Renders

<meta http-equiv="description" content="Bar"/>

      

Example 2:

<head runat="server">
  <meta http-equiv="description" content="<%= Foo %>"/>
</head>

      

Renders:

<meta http-equiv="description" content="&lt;%= Foo %>"/>

      

Note the inconsistency, <

became &lt;

, but >

remains the same.

There are some questions on this topic and the answers are workarounds, but no one seems to know why this is happening.

+1


a source to share


2 answers


You cannot have a script tag (<% =%>) inside a server control, so instead of executing it, it turned into plain text.

You can add a meta tag from code located like this:



HtmlMeta meta = new HtmlMeta();
meta.HttpEquiv = "description";
meta.Content = Foo;
Page.Header.Controls.Add(meta);

      

+4


a source


When you add a runat = server, the Head tag becomes server-side. I assume from your results that the content of the server controls is not parsed for inline lookups using the <% ...%> syntax.



You can also make a server control meta tag by adding the runat = server to it and programmatically accessing its attributes.

+1


a source







All Articles