我们在.NET Core项目中,可以用WebUtility类对Url进行编码和解码,首先我们要确保项目中引入了nuget包:System.Runtime.Extensions
当然这个nuget包默认就是包含在.NET Core的核心库中的,所以正常情况下不用单独去引入。
我们来看看WebUtility类的定义:
using System.IO; namespace System.Net { // // 摘要: // Provides methods for encoding and decoding URLs when processing Web requests. public static class WebUtility { // // 摘要: // Converts a string that has been HTML-encoded for HTTP transmission into a decoded // string. // // 参数: // value: // The string to decode. // // 返回结果: // A decoded string. public static string HtmlDecode(string value); // // 摘要: // Converts a string that has been HTML-encoded into a decoded string, and sends // the decoded string to a System.IO.TextWriter output stream. // // 参数: // value: // The string to decode. // // output: // A System.IO.TextWriter stream of output. // // 异常: // T:System.ArgumentNullException: // The output parameter cannot be null if the value parameter is not null. public static void HtmlDecode(string value, TextWriter output); // // 摘要: // Converts a string to an HTML-encoded string. // // 参数: // value: // The string to encode. // // 返回结果: // An encoded string. public static string HtmlEncode(string value); // // 摘要: // Converts a string into an HTML-encoded string, and returns the output as a System.IO.TextWriter // stream of output. // // 参数: // value: // The string to encode. // // output: // A System.IO.TextWriter output stream. // // 异常: // T:System.ArgumentNullException: // The output parameter cannot be null if the value parameter is not null. public static void HtmlEncode(string value, TextWriter output); // // 摘要: // Converts a string that has been encoded for transmission in a URL into a decoded // string. // // 参数: // encodedValue: // A URL-encoded string to decode. // // 返回结果: // Returns System.String. A decoded string. public static string UrlDecode(string encodedValue); // // 摘要: // Converts an encoded byte array that has been encoded for transmission in a URL // into a decoded byte array. // // 参数: // encodedValue: // A URL-encoded System.Byte array to decode. // // offset: // The offset, in bytes, from the start of the System.Byte array to decode. // // count: // The count, in bytes, to decode from the System.Byte array. // // 返回结果: // Returns System.Byte. A decoded System.Byte array. public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count); // // 摘要: // Converts a text string into a URL-encoded string. // // 参数: // value: // The text to URL-encode. // // 返回结果: // Returns System.String. A URL-encoded string. public static string UrlEncode(string value); // // 摘要: // Converts a byte array into a URL-encoded byte array. // // 参数: // value: // The System.Byte array to URL-encode. // // offset: // The offset, in bytes, from the start of the System.Byte array to encode. // // count: // The count, in bytes, to encode from the System.Byte array. // // 返回结果: // Returns System.Byte. An encoded System.Byte array. public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count); } }
现在我们新建一个.NET Core控制台项目,来演示WebUtility类的简单用法,代码如下:
using System; using System.Net; namespace WebUtilDemo { class Program { static void Main(string[] args) { string rawUri = "http://localhost:8989/home/index"; Console.WriteLine($"原始Uri地址是:{rawUri}"); string encodedUri = WebUtility.UrlEncode(rawUri); Console.WriteLine($"编码后Uri地址是:{encodedUri}");//http%3A%2F%2Flocalhost%3A8989%2Fhome%2Findex string decodedUri = WebUtility.UrlDecode(encodedUri); Console.WriteLine($"解码后Uri地址是:{decodedUri}");//http://localhost:8989/home/index Console.WriteLine("按任意键退出..."); Console.ReadKey(); } } }
程序运行结果如下:
所以我们看到在.NET Core中使用WebUtility类,可以很方便地对Url进行编码和解码。