Sostituzione WebUtility.HtmlDecode in .NET Core


91

Ho bisogno di decodificare i caratteri HTML in .NET Core (MVC6). Sembra che .NET Core non abbia la funzione WebUtility.HtmlDecode che tutti usavano a tale scopo prima. Esiste una sostituzione in .NET Core?



2
@duDE, sta chiedendo .NET Core anziché .NET 4.

Dai un'occhiata alla mia risposta. è la sostituzione di webutility.htmldecode in .net core come httpsputility.

Risposte:


115

Questo è nella classe System.Net.WebUtility (a partire da .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);
}



4
Per .NET Core 2.1 vedere la risposta di Gerardo di seguito, non è necessario installare un altro pacchetto nuget.
Vlad Iliescu

33

Questo è in Net Core 2.0

using System.Text.Encodings.Web;

e chiamalo:

$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(link)}'>clicking here</a>.");

AGGIORNAMENTO : Anche in .Net Core 2.1:

using System.Web;

HttpUtility.UrlEncode(code)
HttpUtility.UrlDecode(code)

Esistono anche metodi HttpUtility.HtmlEncode e HttpUtility.HtmlDecode.
xhafan

16

Ho trovato che la funzione HtmlDecode nella libreria WebUtility funziona.

System.Net.WebUtility.HtmlDecode(string)

3

È necessario aggiungere un riferimento System.Net.WebUtility.

  • È già incluso in .Net Core 2 ( Microsoft.AspNetCore.All)

  • Oppure puoi installare da NuGet , versione di anteprima per .Net Core 1.

Quindi, ad esempio, il tuo codice sarà simile al seguente

public static string HtmlDecode(this string value)
{
     value = System.Net.WebUtility.HtmlDecode(value);
     return value;
}

3
O semplicemente chiama, WebUtility.HtmlDecodenon c'è motivo di includerlo in un metodo di estensione ...
Jamie Rees,

3
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);
    }
}

Puoi usare HttpUtility class in .net coreper la decodifica o la codifica.

spero che funzioni.


Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.