简体   繁体   English

.NET Core 中的 WebUtility.HtmlDecode 替换

[英]WebUtility.HtmlDecode replacement in .NET Core

I need to decode HTML characters in .NET Core (MVC6).我需要在 .NET Core (MVC6) 中解码 HTML 字符。 It looks like .NET Core doesn't have WebUtility.HtmlDecode function which everybody used for that purpose before.看起来 .NET Core 没有 WebUtility.HtmlDecode 函数,之前每个人都用于此目的。 Is there a replacement exist in .NET Core? .NET Core 中是否存在替代品?

This is in the System.Net.WebUtility class (Since .NET Standard 1.0) :这是在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);
}

This is in Net Core 2.0这是在 Net Core 2.0 中

using System.Text.Encodings.Web;

and call it:并称之为:

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

UPDATE : Also in .Net Core 2.1:更新:也在 .Net Core 2.1 中:

using System.Web;

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

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

System.Net.WebUtility.HtmlDecode(string)

You need add reference System.Net.WebUtility .您需要添加参考System.Net.WebUtility

  • It's already included in .Net Core 2 ( Microsoft.AspNetCore.All )它已经包含在 .Net Core 2 ( Microsoft.AspNetCore.All ) 中

  • Or you can install from NuGet - preview version for .Net Core 1.或者您可以从NuGet安装 - .Net Core 1 的预览版。

So example, your code will be look like the below例如,您的代码将如下所示

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

You can use HttpUtility class in .net core for decoding or encoding.您可以使用.net core HttpUtility类进行解码或编码。

hope it will work.希望它会起作用。

HtmlDecode and most of the *Decode methods were not ported to CoreFx. HtmlDecode和大多数*Decode方法没有移植到 CoreFx。 Only the *Encode methods are available.只有*Encode方法可用。

Here's what's available today: https://github.com/dotnet/corefx/blob/1dfe38aeb2811fbbd6d4de36d210f060e80d50a6/src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/HtmlEncoder.cs以下是今天可用的内容: https : //github.com/dotnet/corefx/blob/1dfe38aeb2811fbbd6d4de36d210f060e80d50a6/src/System.Text.Encodings.Web/src/System/Text/Encodings/Web/HtmlEncoder.cs

It isn't the answer, but it is my hint how to solve such kind of issues. 这不是答案,但我的提示是如何解决这类问题。 It is helpful in case only if you are using ReSharper . 仅在您使用ReSharper时才有用。

I had started to develop on .NET Core apps and met a lof of issues like I didn't know a name of packages where my usual classes are located. 我已经开始在.NET Core应用程序上开发并遇到了一些问题,比如我不知道我常用类所在的软件包名称。 ReShareper has the great functionality to solve this: ReShareper具有解决此问题的强大功能:

在此输入图像描述

Check out next article for more details - Finding, Exploring, and Installing NuGet Packages . 查看下一篇文章了解更多详细信息 - 查找,浏览和安装NuGet包 This functionality saved a lot of my time. 这个功能节省了我很多时间。

EDIT: You don't need ReSharper now because Visual Studio 2017 has similar functionality - Visual Studio 2017 can automatically recommend NuGet packages for unknown types. 编辑:您现在不需要ReSharper,因为Visual Studio 2017具有类似的功能 - Visual Studio 2017可以自动推荐未知类型的NuGet包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM