web-dev-qa-db-fra.com

Remplacement WebUtility.HtmlDecode dans .NET Core

Je dois décoder les caractères HTML dans .NET Core (MVC6). Il semble que .NET Core n’ait pas la fonction WebUtility.HtmlDecode que tout le monde utilisait auparavant à cette fin. Existe-t-il un remplacement dans .NET Core?

68
sibvic

C'est dans le System.Net.WebUtility classe:

//
// 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);
}
90
Scott Dorman

C'est dans Net Core 2.0

using System.Text.Encodings.Web;

et appelez ça:

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

UPDATE : Également dans .Net Core 2.1:

using System.Web;

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

J'ai trouvé la fonction HtmlDecode dans la bibliothèque WebUtility qui fonctionne.

System.Net.WebUtility.HtmlDecode(string)
12
pipding

Vous devez ajouter la référence System.Net.WebUtility.

  • Il est déjà inclus dans .Net Core 2 (Microsoft.AspNetCore.All)

  • Ou vous pouvez installer à partir de NuGet - version d’aperçu pour .Net Core 1.

Donc, par exemple, votre code ressemblera à celui ci-dessous

public static string HtmlDecode(this string value)
{
     value = System.Net.WebUtility.HtmlDecode(value);
     return value;
}
2
Nguyễn Top
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);
    }
}

Vous pouvez utiliser HttpUtility class dans .net core pour le décodage ou l’encodage.

j'espère que ça va marcher.

2
user11441779

Ce n’est pas la solution, mais c’est ma suggestion de résoudre ce genre de problèmes. Il est utile dans le cas où vous utilisez ReSharper .

J'avais commencé à développer des applications .NET Core et rencontré beaucoup de problèmes, comme je ne connaissais pas le nom des paquets contenant mes classes habituelles. ReShareper a la grande fonctionnalité pour résoudre ceci:

enter image description here

Consultez l'article suivant pour plus de détails - Recherche, exploration et installation de packages NuGet . Cette fonctionnalité m'a permis de gagner beaucoup de temps.

EDIT: Vous n'avez pas besoin de ReSharper maintenant, car Visual Studio 2017 a des fonctionnalités similaires - Visual Studio 2017 peut automatiquement recommander des packages NuGet pour des types inconnus .

2
RredCat

HtmlDecode et la plupart des *Decode Les méthodes n’ont pas été portées sur CoreFx. Seulement le *Encode méthodes sont disponibles.

Voici ce qui est disponible aujourd'hui: https://github.com/dotnet/corefx/blob/1dfe38aeb2811fbbd6d4de36d210f060e80d50a6/src/System.Text.Encodings.Web/src/System/Text/Encodings HtmlEncoder.cs

1
Victor Hurdugaci