Necesito decodificar caracteres HTML en .NET Core (MVC6). Parece que .NET Core no tiene la función WebUtility.HtmlDecode que todos usaron antes para ese propósito. ¿Existe un reemplazo en .NET Core?
                    
                        c#
                                .net
                                asp.net-core
                                asp.net-core-mvc
                                
                    
                    
                        sibvic
fuente
                
                fuente

Respuestas:
Esto está en la clase System.Net.WebUtility (desde .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); }fuente
Esto está en Net Core 2.0
using System.Text.Encodings.Web;y llámalo:
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(link)}'>clicking here</a>.");ACTUALIZACIÓN : También en .Net Core 2.1:
using System.Web; HttpUtility.UrlEncode(code) HttpUtility.UrlDecode(code)fuente
Encontré que la función HtmlDecode en la biblioteca WebUtility funciona.
System.Net.WebUtility.HtmlDecode(string)fuente
Necesita agregar una referencia
System.Net.WebUtility.Ya está incluido en .Net Core 2 (
Microsoft.AspNetCore.All)O puede instalar desde NuGet - versión preliminar para .Net Core 1.
Por ejemplo, su código se verá como el siguiente
public static string HtmlDecode(this string value) { value = System.Net.WebUtility.HtmlDecode(value); return value; }fuente
WebUtility.HtmlDecodeno hay razón para envolverlo en un método de extensión ...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); } }Puede usar
HttpUtilityclass in.net corepara decodificar o codificar.Espero que funcione.
fuente
HtmlDecodey la mayoría de los*Decodemétodos no se portaron a CoreFx. Solo los*Encodemétodos están disponibles.Esto es lo que está disponible hoy: https://github.com/dotnet/corefx/blob/1dfe38aeb2811fbbd6d4de36d210f060e80d50a6/src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/HtmlEncoder.cs
fuente