I've been going through and converting my project to use the <%:
instead of the <%=
syntax, and have been annoyed that I also have to remove Html.Encode(zoozle)
all over the place. If it's left in place, zoozle
will get double-encoded.
This leaves me wondering, why doesn't Html.Encode
return HtmlString
, so that it doesn't get encoded again by <%:
?
The HtmlString is a new class introduced in the .NET 4.0 framework. The HtmlEncode method is on the HttpServerUtility class that has been around since .NET 1.1. Microsoft probably didn't change it so as not to break all the previous implementations that rely on this method returning a string. They can't overload it because you can't overload solely on the return type of a method.
Phil Haacked has a regex that can be used to replace all instances of Html.Encode. http://haacked.com/archive/2010/04/29/replacing-html-encode.aspx