Tuesday, March 4, 2014

Create a meta tag with ASP.NET HtmlMeta control double encode content

So I have a physical page template in Umbraco which generates any number of Web pages/URLs dynamically from a database, and decided that I'm going to create bespoke content for various SEO Meta tags depending on the page content.

I had a field in my database with SEO tags already HTML encoded-content: Thunder & Lightning, "very, very, scary" read up, the correct way to add a meta tag on the pagein c # (and therefore the razor in Umbraco) goes a little like this: 1: page = HttpContext.Current.Handler as page; 2: HtmlMeta meta description = new HtmlMeta (); 3: metaDescription.Name = "Description"; 4. meta description Content = "my description"; 5: page.Header.Controls.Add (meta description); That worked, but I found this was for output to the Web browser: Thunder & amp; Lightning & quot; very, very, scary & quot;ASP.NET seems to be HTML encoding to a string that is already HTML encoded, resulting in the above mess.

Could not find an obvious way to stop the above code from performing unnecessary HTML coding, so screw makes it properly ... here's my working solution:

1: page = HttpContext.Current.Handler as page; 2: LiteralControl litMetaDescription = new LiteralControl (); 3: litMetaDescription .text = " "); 4: page.Header.Controls.Add (litMetaDescription); If you are sure that you pass the string is properly HTML encoded, above will work fine.

iPage Reviews: Get $10 Cashback Now

No comments:

Post a Comment