在.NET Core中替代WebUtility.HtmlDecode的方法

122

我需要在.NET Core(MVC6)中解码HTML字符。看起来.NET Core没有之前大家用于此目的的WebUtility.HtmlDecode函数。在.NET Core中是否存在替代方法?


1
请查看:https://msdn.microsoft.com/library/73z22y6h%28v=vs.100%29.aspx - Leo Chapiro
2
@duDE,他在询问的是.NET Core而不是.NET 4。 - user1108948
请看我的回答。它是 .NET Core 中 WebUtility.HtmlDecode 的替代品,即使用 HttpUtility.HtmlDecode。 - user11441779
警告:在搜索和替换时,请小心不要混淆HtmlEncodeUrlEncode,特别是如果您一次更改多个。一个用于HTML标记,另一个用于URL。当我搜索和替换时,我差点犯了这个愚蠢的错误! - Simon_Weaver
6个回答

141

这是在 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);
}

1
NuGet包 https://www.nuget.org/packages/Microsoft.AspNet.WebUtilities/ - Frank Myat Thu
11
对于.NET Core 1.1,请使用https://www.nuget.org/packages/Microsoft.AspNetCore.WebUtilities。 - wolfyuk
5
对于.NET Core 2.1,请参阅下面Gerardo的回复,无需安装另一个nuget包。 - Vlad Iliescu

47

这是在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。


1
还有 HttpUtility.HtmlEncode 和 HttpUtility.HtmlDecode 方法。 - xhafan
1
@xhafan 自2010年ASP.NET 4.0以来,这些方法只是调用System.Net.WebUtility.HtmlEncode(直接在4.0中或通过HttpEncoder在4.5中)。 - Dai

25

我发现WebUtility库中的HtmlDecode函数可用。

System.Net.WebUtility.HtmlDecode(string)

5
您需要添加引用 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;
}

3
只需调用WebUtility.HtmlDecode,没有必要将其包装在扩展方法中... - Jamie Rees

4
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:https://learn.microsoft.com/en-us/dotnet/api/system.web.httputility?view=net-6.0 - Dai

2

这个答案来自2016年,已经不适用于.NET Core 3.1或更高版本。 - Dai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接