Mark Gilbert's Blog

Science and technology, served light and fluffy.

Meta Insanity 2 – Strings and Meta Tags revisited

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”.

This past week, at my functional language users group meeting (FLUNK),  we took a break from doing problems or talks on functional programming, and instead did lightning talks about something tech-related.  I built my talk around my October blog post.  The talk went great – everyone got into trying other combinations of markup to either 1) figure out why it was failing, or 2) figure out better ways to get around it than just appending empty strings.  What follows are some of the other things we discovered that worked and didn’t work.

For this list, “worked” means that a tag such as the following rendered the expression hole correctly:

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

While “didn’t work” means that the leading angle bracket (<) was HTML encoded before the expression hole is evaluated, which led to rendered markup like this:

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

  1. Moving the meta tag into the <body> tag worked.
  2. Putting a different tag with an expression hole, such as <script>, or even fake ones like <blah>, worked.
  3. Removing the runat=”server” attribute from the <head> tag worked for everything, including <meta> tags.
  4. Removing the double quotes from the markup, and adding them to the string that would be returned in the expression hole, worked.
  5. Adding attributes to the <head> tag didn’t work.
  6. Adding attributes to the <head> tag, and removing the runat=”server” attribute, worked.

And probably the best find of the evening (and by “best” I mean “face-palm-funny”) was HTML encoding the double quotes in the source markup, like this:

<meta name="description6" content=&quot;<%=Me.SomeMetaValue%>&quot; />

The rendered result?  The quotes stayed encoded, but the expression hole was properly evaluated:

<meta name=”description6″ content=&quot;Blah blah&quot; />
So, what did we accomplish?  It looks like the combination of runat=”server” and <meta> tags is the crux of this issue.  Meta tags will always be needed on a professional site, but I can’t remember the last time I needed to access the <head> tag from the server, so it seems like simply removing that attribute is now the cleanest way to get around this issue.

Advertisements

May 12, 2012 - Posted by | Visual Studio/.NET

Sorry, the comment form is closed at this time.

%d bloggers like this: