首页 .Net ASP.NET Core(.NET Core)中使用HtmlDecode和HtmlEncode方法

ASP.NET Core(.NET Core)中使用HtmlDecode和HtmlEncode方法

1、.NET Core 1.1中使用

在.NET Core 1.1中使用HtmlDecode和HtmlEncode方法,可能需要安装Nuget包,安装方法如下,

PM> Install-Package Microsoft.AspNetCore.WebUtilities -Version 2.1.1

命令空间:System.Net.WebUtility

//
// Summary:
//     Provides methods for encoding and decoding URLs when processing Web requests.
public static >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);
}

使用方法

WebUtility.HtmlEncode(code)
WebUtility.HtmlEncode(code)

2、.Net Core 2.0中使用

在Net Core 2.0中,WebUtility类和下面两种方法都可以直接使用,不需要安装NuGet包。

1)命名空间:using System.Text.Encodings.Web;

使用方法

HtmlEncoder.Default.Encode(code)
HtmlEncoder.Default.Decode(code)

2)命名空间:using System.Web;

使用方法

HttpUtility.HtmlDecode(code)
HttpUtility.HtmlEncode(code)
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。