Post Pic

ASP.NET Duplicate Title Tags

A client recently highlighted a bug in an Umbraco site I had developed that was causing duplicate title tags to be inserted and resulted in Accessibility validation failure for the client. It seems that there is a bug(?) in .Net when using a <head> tag with the runat=”server” attribute, if it does not encounter a¬†HtmlTitle control in the child controls collection it will insert one for you which in this case has undesirable results. On the site in question there is a macro in the master template header that generates title tags for pages so I had to find a way around it.

The solution it turns out is extremely simple, a bit of hack nonetheless but it works.

<title visible="false" runat="server" />

All we need to do is to trick the framework by adding a title tag with the runat=”server” attribute and set it’s visibilty to “false” so that it doesn’t get rendered – job done!

Of course some of you will be sitting there thinking, “Why don’t you just remove the runat=”server” attribute from the header” – well, in this instance that is not an option since I have other stuff going on in the header that requires it but it is another viable solution – if you don’t need it, simply remove it from the header tag and this should also resolve your problem.

If you want to read further on this topic try reading ‘Title Tags and Master Pages’ by Phil Haack.

Umbraco Certified Developer
SagePay Approved Partner
Creative Market