【问题标题】:WebUtility.HtmlDecode replacement in .NET Core.NET Core 中的 WebUtility.HtmlDecode 替换
【发布时间】:2016-02-16 16:13:16
【问题描述】:

我需要在 .NET Core (MVC6) 中解码 HTML 字符。看起来 .NET Core 没有 WebUtility.HtmlDecode 函数,以前每个人都用于此目的。 .NET Core 中是否存在替代品?

【问题讨论】:

标签: c# .net asp.net-core asp.net-core-mvc


【解决方案1】:

这是在 System.Net.WebUtility 类中(自 .NET Standard 1.0 起):

//
// Summary:
//     Provides methods for encoding and decoding URLs when processing Web requests.
public static class WebUtility
{
    public static string HtmlDecode(string value);
    public static string HtmlEncode(string value);
    public static string UrlDecode(string encodedValue);
    public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count);
    public static string UrlEncode(string value);
    public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count);
}

【讨论】:

【解决方案2】:

这是在 Net Core 2.0 中

using System.Text.Encodings.Web;

然后调用它:

$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(link)}'>clicking here</a>.");

更新: 同样在 .Net Core 2.1 中:

using System.Web;

HttpUtility.UrlEncode(code)
HttpUtility.UrlDecode(code)

更新: 也适用于 .NET Core 3.1 到 .NET 5 和 .NET 6。

【讨论】:

  • 还有HttpUtility.HtmlEncode和HttpUtility.HtmlDecode方法。
  • @xhafan 从 2010 年的 ASP.NET 4.0 开始,这些方法只需调用 System.Net.WebUtility.HtmlEncode(在 4.0 中直接调用,或在 4.5 中通过 HttpEncoder)。
【解决方案3】:

我发现 WebUtility 库中的 HtmlDecode 函数可以工作。

System.Net.WebUtility.HtmlDecode(string)

【讨论】:

    【解决方案4】:

    您需要添加参考System.Net.WebUtility

    • 它已经包含在 .Net Core 2 中 (Microsoft.AspNetCore.All)

    • 或者您可以从 NuGet 安装 - .Net Core 1 的预览版。

    例如,您的代码将如下所示

    public static string HtmlDecode(this string value)
    {
         value = System.Net.WebUtility.HtmlDecode(value);
         return value;
    }
    

    【讨论】:

    • 或者只是调用WebUtility.HtmlDecode 没有理由将其包装在扩展方法中......
    【解决方案5】:
    namespace System.Web
    {
        //
        // Summary:
        //     Provides methods for encoding and decoding URLs when processing Web requests.
        //     This class cannot be inherited.
        public sealed class HttpUtility
        {
            public HttpUtility();
            public static string HtmlAttributeEncode(string s);
            public static void HtmlAttributeEncode(string s, TextWriter output); 
            public static string HtmlDecode(string s);
            public static void HtmlDecode(string s, TextWriter output);
            public static string HtmlEncode(string s);
            public static string HtmlEncode(object value);
            public static void HtmlEncode(string s, TextWriter output);
            public static string JavaScriptStringEncode(string value);
            public static string JavaScriptStringEncode(string value, bool addDoubleQuotes);
            public static NameValueCollection ParseQueryString(string query);
            public static NameValueCollection ParseQueryString(string query, Encoding encoding);
            public static string UrlDecode(string str, Encoding e);
            public static string UrlDecode(byte[] bytes, int offset, int count, Encoding e);
            public static string UrlDecode(string str);
            public static string UrlDecode(byte[] bytes, Encoding e);
            public static byte[] UrlDecodeToBytes(byte[] bytes, int offset, int count);
            public static byte[] UrlDecodeToBytes(string str, Encoding e);
            public static byte[] UrlDecodeToBytes(byte[] bytes);
            public static byte[] UrlDecodeToBytes(string str);
            public static string UrlEncode(string str);
            public static string UrlEncode(string str, Encoding e);
            public static string UrlEncode(byte[] bytes);
            public static string UrlEncode(byte[] bytes, int offset, int count);
            public static byte[] UrlEncodeToBytes(string str);
            public static byte[] UrlEncodeToBytes(byte[] bytes);
            public static byte[] UrlEncodeToBytes(string str, Encoding e);
            public static byte[] UrlEncodeToBytes(byte[] bytes, int offset, int count);
            [Obsolete("This method produces non-standards-compliant output and has interoperability issues. The preferred alternative is UrlEncode(String).")]
            public static string UrlEncodeUnicode(string str);
            [Obsolete("This method produces non-standards-compliant output and has interoperability issues. The preferred alternative is UrlEncodeToBytes(String).")]
            public static byte[] UrlEncodeUnicodeToBytes(string str);
            public static string UrlPathEncode(string str);
        }
    }
    

    您可以使用.net core 中的HttpUtility 类进行解码或编码。

    希望它会起作用。

    【讨论】:

    • 此答案仅适用于 .NET Framework、.NET Core 3.1 和 .NET 5(及更高版本),其他版本的 .NET(如 .NET Core 1.x 和 2.x)适用没有System.Web.HttpUtility:docs.microsoft.com/en-us/dotnet/api/…
    【解决方案6】:

    HtmlDecode 和大多数 *Decode 方法未移植到 CoreFx。只有*Encode 方法可用。

    这是今天可用的内容:https://github.com/dotnet/corefx/blob/1dfe38aeb2811fbbd6d4de36d210f060e80d50a6/src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/HtmlEncoder.cs

    【讨论】:

    • 此答案来自 2016 年,不再适用于 .NET Core 3.1 或更高版本。
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    相关资源
    最近更新 更多