So I have one physical page/template in Umbraco that generates any number of web pages/Urls dynamically from a database, and decided that I should create bespoke content for the various SEO meta tags depending on the content of the page.
I had a field in my database with the SEO tags already HTML encoded - with content like:
I had a field in my database with the SEO tags already HTML encoded - with content like:
Thunder & Lightning, "very, very, frightening"Reading up, the correct way to add a meta tag to the page <head> in c# (and therefore Razor in Umbraco) goes a little bit like this:
1: Page page = HttpContext.Current.Handler as Page;
2: HtmlMeta metaDescription = new HtmlMeta();
3: metaDescription.Name = "description";
4: metaDescription.Content = "My description content";
5: page.Header.Controls.Add(metaDescription);
Which worked, only I found this was being output to the browser:Thunder &amp; Lightning, &quot; very, very, frightening&quot;
ASP.NET seems to be HTML encoding a string that's already HTML encoded, leading to the above mess.
Couldn't find an obvious way to stop the above code from carrying out the unnecessary HTML encoding, so screw doing it properly... here is my working solution:
Couldn't find an obvious way to stop the above code from carrying out the unnecessary HTML encoding, so screw doing it properly... here is my working solution:
1: Page page = HttpContext.Current.Handler as Page;
2: LiteralControl litMetaDescription = new LiteralControl();
3: litMetaDescription.Text = "<meta name=\"description\" content=\"Description\"/>");
4: page.Header.Controls.Add(litMetaDescription);
Providing you are sure that the string you are passing is properly HTML encoded, the above will work fine.
Comments
Post a Comment