Asp.net 不同.net framework HtmlEncode方法之间的差异

Asp.net 不同.net framework HtmlEncode方法之间的差异,asp.net,.net,Asp.net,.net,以下各项之间有什么区别(如有): HttpUtility.HtmlEncode和HttpServerUtility.HtmlEncode都表示: 要在web应用程序之外对值进行编码或解码,请使用类 这意味着有区别,但不要直接说出来,或者说区别是什么,如果有区别的话。如果你深入研究,你可以很容易地理解 System.Web.HttpUtility.HtmlEncode 所以System.Web.HttpServerUtility.HtmlEncode实际上使用System.Web.HttpU

以下各项之间有什么区别(如有):



HttpUtility.HtmlEncode和HttpServerUtility.HtmlEncode都表示:

要在web应用程序之外对值进行编码或解码,请使用类

这意味着有区别,但不要直接说出来,或者说区别是什么,如果有区别的话。

如果你深入研究,你可以很容易地理解

System.Web.HttpUtility.HtmlEncode 所以
System.Web.HttpServerUtility.HtmlEncode
实际上使用
System.Web.HttpUtility.HtmlEncode
。如果钻取
HttpEncoder.Current.HtmlEncode这有以下代码:

    protected internal virtual void HtmlDecode(string value, TextWriter output) {
        WebUtility.HtmlDecode(value, output);
    }
Tl;博士
因此,他们最终都使用
System.Net.WebUtility.HtmlEncode
。我想
System.Web
版本只是为了向后兼容。因此建议使用
System.Net
版本。

为什么不检查源代码。如果您不想将
System.Web
作为参考并保留一个客户机,那么Net基本上就是这样profile@Liam:两个原因。我不习惯认为.net源代码是可用的,我认为这是一个很好的问题。所以,尽管投了反对票,我不会删除它。特别是在得到了很好的回答之后,很好的回答。我注意到HttpUtility.Htmlencode可能使用不同的(非默认)编码器。希望IIS不会创建一个。
    /// <devdoc>
    ///    <para>
    ///       HTML
    ///       encodes a given string and
    ///       returns the encoded string.
    ///    </para>
    /// </devdoc>
    public string HtmlEncode(string s) {
        return HttpUtility.HtmlEncode(s);
    }
    public static string HtmlEncode(string value) {
        if (String.IsNullOrEmpty(value)) {
            return value;
        }

        // Don't create string writer if we don't have nothing to encode
        int index = IndexOfHtmlEncodingChars(value, 0);
        if (index == -1) {
            return value;
        }

        StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
        HtmlEncode(value, writer);
        return writer.ToString();
    }
    protected internal virtual void HtmlDecode(string value, TextWriter output) {
        WebUtility.HtmlDecode(value, output);
    }