.NET Core中的WebUtility.HtmlDecode替换


91

我需要在.NET Core(MVC6)中解码HTML字符。看起来.NET Core没有以前每个人都用于此目的的WebUtility.HtmlDecode函数。.NET Core中是否存在替代品?



2
@duDE,他要求.NET的核心,而不是.NET 4

看看我的答案。它是.net核心中的webutility.htmldecode的替换为httputility.HtmlDecode。

Answers:


115

这在System.Net.WebUtility类中(自.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
对于.NET Core 2.1,请参见下面的Gerardo响应,无需安装其他nuget软件包。
弗拉德·伊利埃斯库

33

这是在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)

还有HttpUtility.HtmlEncode和HttpUtility.HtmlDecode方法。
xhafan

16

我发现WebUtility库中的HtmlDecode函数可以正常工作。

System.Net.WebUtility.HtmlDecode(string)

3

您需要添加参考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;
}

3
或者只是打电话WebUtility.HtmlDecode,没有理由将其包装在扩展方法中……
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);
    }
}

您可以使用HttpUtility class in.net core进行解码或编码。

希望它能工作。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.