为什么 html.encode 不返回 HtmlString
我一直在检查并将我的项目转换为使用 <%:
而不是 <%=
语法,并且很恼火的是我还必须删除Html.Encode(zoozle)
遍布各处。如果将其保留在原处,zoozle
将进行双重编码。
这让我想知道,为什么 Html.Encode
不返回 HtmlString
,这样它就不会被 <%:
再次编码?
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 <%:
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HtmlString 是 .NET 中引入的新类4.0框架。 HtmlEncode 方法位于 HttpServerUtility 类上,该类自 .NET 1.1 以来就已存在。微软可能没有改变它,以免破坏之前依赖此方法返回字符串的所有实现。它们无法重载它,因为您不能仅重载方法的返回类型。
Phil Haacked 有一个正则表达式,可用于替换 Html.Encode 的所有实例。 http://haacked.com/archive/2010/04/29 /替换-html-encode.aspx
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