我需要在.NET Core(MVC6)中解码HTML字符。看起来.NET Core没有之前大家用于此目的的WebUtility.HtmlDecode函数。在.NET Core中是否存在替代方法?
我需要在.NET Core(MVC6)中解码HTML字符。看起来.NET Core没有之前大家用于此目的的WebUtility.HtmlDecode函数。在.NET Core中是否存在替代方法?
这是在 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);
}
这是在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。
System.Net.WebUtility.HtmlEncode
(直接在4.0中或通过HttpEncoder
在4.5中)。 - Dai我发现WebUtility库中的HtmlDecode函数可用。
System.Net.WebUtility.HtmlDecode(string)
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
,没有必要将其包装在扩展方法中... - Jamie Reesnamespace 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
类进行编码或解码。
希望这能起作用。
System.Web.HttpUtility
:https://learn.microsoft.com/en-us/dotnet/api/system.web.httputility?view=net-6.0 - DaiHtmlDecode
和大多数*Decode
方法未被移植到CoreFx。仅有*Encode
方法可用。
HtmlEncode
和UrlEncode
,特别是如果您一次更改多个。一个用于HTML标记,另一个用于URL。当我搜索和替换时,我差点犯了这个愚蠢的错误! - Simon_Weaver