Мне нужно декодировать HTML-символы в .NET Core (MVC6). Похоже, что .NET Core не имеет функции WebUtility.HtmlDecode, которую раньше использовали для этой цели. Есть ли замена в .NET Core?
Замена WebUtility.HtmlDecode в .NET Core
Ответ 1
Это в классе System.Net.WebUtility
:
//
// 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)
Ответ 3
Я нашел функцию HtmlDecode в библиотеке WebUtility для работы.
System.Net.WebUtility.HtmlDecode(string)
Ответ 4
Это не ответ, но это мой намек на то, как решить такие проблемы. Это полезно в случае, если вы используете ReSharper.
Я начал разрабатывать приложения .NET Core и встречал множество проблем, таких как я не знал названия пакетов, где находятся мои обычные классы. ReShareper обладает большой функциональностью для решения этой проблемы:
За дополнительной информацией обратитесь к следующей статье - Поиск, исследование и установка пакетов NuGet. Эта функциональность сэкономила много времени.
EDIT: Теперь вам не нужен ReSharper, потому что Visual Studio 2017 имеет схожие функции - Visual Studio 2017 может автоматически рекомендовать NuGet пакеты для неизвестных типов.
Ответ 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;
}
Ответ 6
HtmlDecode
, и большинство методов *Decode
не были перенесены в CoreFx. Доступны только методы *Encode
.
Здесь то, что доступно сегодня: https://github.com/dotnet/corefx/blob/1dfe38aeb2811fbbd6d4de36d210f060e80d50a6/src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/HtmlEncoder.cs
Ответ 7
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);
}
}
Вы можете использовать класс HttpUtility
в .net core
для декодирования или кодирования.
надеюсь, что это сработает.