2016-02-16 48 views
39

Devo decodificare i caratteri HTML in .NET Core (MVC6). Sembra che .NET Core non abbia la funzione WebUtility.HtmlDecode che tutti hanno usato prima per questo scopo. Esiste una sostituzione in .NET Core?WebUtility.HtmlDecode replacement in .NET Core

+1

Date un'occhiata: https://msdn.microsoft.com/library/73z22y6h%28v= vs.100% 29.aspx – duDE

+2

@duDE, chiede .NET Core anziché .NET 4. –

risposta

61

Questo è nella classe 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); 
} 
+1

pacchetto nuget https://www.nuget.org/packages/Microsoft.AspNet.WebUtilities/ –

+8

Per .NET Core 1.1 utilizzare https://www.nuget.org/packages/Microsoft. AspNetCore.WebUtilities – WolfyUK

0

E non è la risposta, ma è il mio suggerimento come risolvere questo tipo di problemi. È utile nel caso in cui solo se si utilizza ReSharper.

Avevo iniziato a sviluppare su app .NET Core e ho incontrato un sacco di problemi come non sapevo un nome di pacchetti in cui si trovano le mie solite classi. ReShareper ha la grande funzionalità per risolvere questo:

enter image description here

Partenza prossimo articolo per maggiori dettagli - Finding, Exploring, and Installing NuGet Packages. Questa funzionalità mi ha salvato molto del mio tempo.

EDIT: Non è necessario ReSharper ora perché Visual Studio 2017 ha funzionalità simili - Visual Studio 2017 can automatically recommend NuGet packages for unknown types.

8

ho trovato la funzione HtmlDecode nella libreria WebUtility al lavoro.

System.Net.WebUtility.HtmlDecode(string) 
1

Questo è in netto core 2,0

using System.Text.Encodings.Web; 

e chiamarlo:

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