Mark Gilbert's Blog

Science and technology, served light and fluffy.

Meta Insanity – Strings and Meta Tags

It was another one of those days.  And this time, I DID go home after I saw this in action.

On many of my past Web Forms sites, I’ve had to include a dynamic meta/description tag in the header.  The “dynamic” parts come in when the page being rendered is a product page, a recipe page, or something else where the data is drawn out of a database.

When I first started doing this, I tried something like this*:

<meta name="description1" content="<%=Me.SomeMetaValue%>" />


Where “SomeMetaValue” is a property of the page, or a reference to a shared value somewhere else in the solution.  This version fails because the inner left angle bracket gets encoded, thus ruining the server-side expression hole:

<meta name=”description1″ content=”&lt;%=Me.SomeMetaValue%>” />

To get around this, I replaced the entire meta/description tag with an ASP.NET literal, and insert the value I wanted on the server side.  That worked.

Recently, this issue came up again.  Doug had taken over one of the sites where I had done this, and he refused to believe that the data wouldn’t render correctly in the expression hole.  He tried it, and sure enough the < got encoded.  Doug kept at it, though, and found that if you removed the double quotes around the expression hole, it rendered the value correctly:

<meta name="description2" content=<%=Me.SomeMetaValue%> />

Unfortunately, this was no longer valid HTML:

<meta name=”description2″ content=Blah blah />

Doug remembered an old issue he and I troubleshot, documented at System.String has me in knots.  He modified the original attempt to prepend an empty string:

<meta name="description3" content="<%="" & Me.SomeMetaValue%>" />


That renders perfectly:

<meta name=”description3″ content=”Blah blah” />

Putting the empty string after SomeMetaValue also works just fine:

<meta name=”description3″ content=”<%=Me.SomeMetaValue & “”%>” />




I had written off the encoded angle bracket to an overly eager web server (Cassini and IIS behaved the same in this case), but why in the world would tacking on an empty string force it to NOT be encoded and allow it to work?  Since we’re dealing with silly strings here, I followed the lesson learned in System.String has me in knots, and added .ToString onto the end of Me.SomeMetaValue:

<meta name="description4" content="<%=Me.SomeMetaValue.ToString%>" />



Aaaaaand, we’re back to encoding:

<meta name=”description4″ content=”&lt;%=Me.SomeMetaValue.ToString%>” />

Sigh.  Oh Visual Studio.  Why must you taunt me so?

For a fully working – er, FAILING – sample of the above, check out the archive at


* I used “description1”, “description2”, etc. just for illustrative purposes for this blog post.  The production sites have this value as simply “description”.


October 19, 2011 - Posted by | Visual Studio/.NET

1 Comment

  1. […] In October of 2011, I posted about some funkiness with trying to embed an expression hole into the "content" attribute of a <meta> tag.  I dubbed it "Meta Insanity". […]

    Pingback by Meta Insanity 2 – Strings and Meta Tags revisited « Mark Gilbert’s Blog | May 12, 2012

Sorry, the comment form is closed at this time.

%d bloggers like this: