.NET Core中如何对Url进行编码和解码

我们在.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中如何对Url进行编码和解码

所以我们看到在.NET Core中使用WebUtility类,可以很方便地对Url进行编码和解码。

WebUtility.UrlEncode和HttpUtility.UrlEncode的空格转换问题

目前.NET Core中WebUtility.UrlEncode方法会将空格" "转换为加号"+",然而空格" "的Url编码应该是"%20",不知道这是不是目前.NET Core的一个Bug,因为这个问题实际上在WebUtility.UrlEncode和HttpUtility.UrlEncode两个方法中都存在。

但是也有说法,在Url编码中空格" ",既可以用加号"+"表示,也可以用"%20"表示,所以这也不一定是个Bug。

如果要将空格" "编码为"%20",可以使用Uri.EscapeDataString方法:

using System;
using System.Net;
using System.Web; namespace WebUtilDemo
{
class Program
{
static void Main(string[] args)
{
string rawUri = "http://ABC EFG"; Console.WriteLine($"原始Uri地址是:{rawUri}"); string encodedUri1 = WebUtility.UrlEncode(rawUri);
string encodedUri2 = HttpUtility.UrlEncode(rawUri); Console.WriteLine($"encodedUri1 是 {encodedUri1}");//http%3A%2F%2FABC+EFG
Console.WriteLine($"encodedUri2 是 {encodedUri2}");//http%3a%2f%2fABC+EFG string decodedUri1 = WebUtility.UrlDecode(encodedUri1);
string decodedUri2 = WebUtility.UrlDecode(encodedUri2); Console.WriteLine($"decodedUri1 是 {decodedUri1}");//http://ABC EFG
Console.WriteLine($"decodedUri2 是 {decodedUri2}");//http://ABC EFG string encodedUri3 = Uri.EscapeDataString(rawUri);
Console.WriteLine($"encodedUri3 是 {encodedUri3}");//http%3A%2F%2FABC%20EFG string decodedUri3 = WebUtility.UrlDecode(encodedUri3);
Console.WriteLine($"decodedUri3 是 {decodedUri3}");//http://ABC EFG Console.WriteLine("按任意键退出...");
Console.ReadKey();
}
}
}

这个问题可以参考:

URL Encode and Decode in ASP.NET Core

上一篇:Failed to start component [StandardEngine [Catalina].StandardHost[localhost].StandardContext[/项目名]]


下一篇:python - 常用模块(3)