Wednesday, 2 March 2016

Umbraco 7.4.1 Hosting Tips: How to Create a Meta Tag using ASP.NET HtmlMeta Control Double Encodes Content?

Leave a Comment
In this Umbraco hosting Tips, I will tell you about How to Create a Meta Tag using ASP.NET HtmlMeta Control double encodes content. 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.

Umbraco 7.4.1 Hosting Tips: How to Create a Meta Tag using ASP.NET HtmlMeta Control Double Encodes Content?

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:
Page page = HttpContext.Current.Handler as Page;
HtmlMeta metaDescription = new HtmlMeta();
metaDescription.Name = "description";
metaDescription.Content = "My description content";
page.Header.Controls.Add(metaDescription);

Which worked, only I found this was being output to the browser:
Thunder & Lightning, " very, very, frightening"

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:
Page page = HttpContext.Current.Handler as Page;
LiteralControl litMetaDescription = new LiteralControl();
litMetaDescription.Text = "<meta name=\"description\" content=\"Description\"/>");
page.Header.Controls.Add(litMetaDescription);

Providing you are sure that the string you are passing is properly HTML encoded, the above will work fine.

Best Umbraco 7.4.1 Hosting Recommendation

One of the most important things when choosing a good Umbraco 7.4.1 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable Umbraco 7.4.1, their servers are optimized for PHP web applications such as the latest Umbraco version. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast Umbraco hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFE.eu guarantees 99.9% uptime for Umbraco. And the engineers do regular maintenance and monitoring works to assure its Umbraco hosting are security and always up. 

http://hostforlifeasp.net

0 comments:

Post a Comment