WebUtility.HtmlDecode replacement in .NET Core

I need to decode HTML characters in .NET Core (MVC6). It looks like .NET Core doesn't have WebUtility.HtmlDecode function which everybody used for that purpose before. Is there a replacement exist in .NET Core?

63733 次浏览

HtmlDecode和大多数 *Decode方法都没有移植到 CoreFx 上,只有 *Encode方法可用。

以下是今天有售的: https://github.com/dotnet/corefx/blob/1dfe38aeb2811fbbd6d4de36d210f060e80d50a6/src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/HtmlEncoder.cs

这属于 系统网络工具类(自.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);
}

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

System.Net.WebUtility.HtmlDecode(string)

这是在网络核心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。

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

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

  • 或者您可以从 NuGet安装-预览版本的.NetCore1。

例如,您的代码如下所示

public static string HtmlDecode(this string value)
{
value = System.Net.WebUtility.HtmlDecode(value);
return value;
}
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类进行解码或编码。

希望能成功。