ASP.NET Core 中的 URL 编码和解码

问题描述 投票:0回答:7
HttpContext.Current.Server.UrlEncode

这仅适用于 .NET Framework。如何在 ASP.NET Core 中对 URI 参数进行编码或解码?

c# asp.net-core urlencode
7个回答
308
投票
  • 对于 ASP.NET Core 2.0+,只需添加

    System.Net
    命名空间 -
    WebUtility
    类作为
    System.Runtime.Extensions
    nuget 包的一部分提供,默认情况下在 ASP.NET Core 项目中引用。

  • 对于之前的版本添加

    Microsoft.AspNetCore.WebUtilities
    nuget 包。

然后

WebUtility
课程将为您提供:

public static class WebUtility
{
    public static string UrlDecode(string encodedValue);
    public static string UrlEncode(string value);
}

107
投票

对于 ASP.Net Core 2.0+ 并且如果您需要将空格编码为

%20

+
相反;

用途:

 Uri.EscapeDataString(someString);

100
投票

它可在 .Net Core SDK 版本 2.0.0 中使用,位于 System.Net.WebUtility.UrlEncode

请参阅文档


15
投票
只是添加另一种方法:

UrlEncoder.Default.Encode(url);

https://learn.microsoft.com/en-us/dotnet/api/system.text.encodings.web.urlencoder?view=net-6.0


7
投票
我正在使用

redirect,UrlEncode 对我不起作用,因为它对整个 url 进行编码。我通过使用 UriHelper.Encode 解决了这个问题,如下所示。

UriHelper.Encode

// generate url string... return Redirect(Microsoft.AspNetCore.Http.Extensions.UriHelper.Encode(new System.Uri(url)));
    

0
投票

RFC3986 定义哪些字符是保留的和未保留的。

保留字符:

! # $ & ‘ ( ) * + , / : ; = ? @ [ ]
未保留字符:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 – _ . ~
我们对 URL 中的保留字符进行编码,.NET 提供了几种编码和解码的方法。

使用 HttpUtility 类

var url = @"http://example.com/resource?foo=bar with space#fragment"; var httpUtilityEncoded = HttpUtility.UrlEncode(url); Console.WriteLine(httpUtilityEncoded); //http%3a%2f%2fexample.com%2fresource%3ffoo%3dbar+with+space%23fragment var httpUtilityDecoded = HttpUtility.UrlDecode(httpUtilityEncoded); Console.WriteLine(httpUtilityDecoded); //http://example.com/resource?foo=bar with space#fragment
使用 WebUtility 类

var url = @"http://example.com/resource?foo=bar with space#fragment"; var webUtilityEncoded = WebUtility.UrlEncode(url); Console.WriteLine(webUtilityEncoded); //http%3A%2F%2Fexample.com%2Fresource%3Ffoo%3Dbar+with+space%23fragment var webUtilityDecoded = WebUtility.UrlDecode(webUtilityEncoded); Console.WriteLine(webUtilityDecoded); //http://example.com/resource?foo=bar with space#fragment
如果我们不在 Web 应用程序中,请考虑使用 WebUtility 类 

如此处记录

使用 Uri 类

var url = @"http://example.com/resource?foo=bar with space#fragment"; var uriEncoded = Uri.EscapeDataString(url); Console.WriteLine(uriEncoded); //http%3A%2F%2Fexample.com%2Fresource%3Ffoo%3Dbar%20with%20space%23fragment var uriDecoded = Uri.UnescapeDataString(uriEncoded); Console.WriteLine(uriDecoded); //http://example.com/resource?foo=bar with space#fragment
注意事项

请注意,

HttpUtility.UrlEncode()

生成保留字符的小写编码,而
WebUtility.UrlEncode()
Uri.EscapeDataString()
均输出大写。

此外,

HttpUtility.UrlEncode()

WebUtility.UrlEncode()
都将空格字符编码为“+”,而
Uri.EscapeDataString()
将空格编码为“%20”。

Uri.EscapeDataString()

 的字符数限制为 32766 个,如果我们尝试编码的字符数超过该限制,则会抛出 
UriFormatException。因此,如果我们确实需要对特别长的 URL 进行编码,我们可能需要使用 HttpUtility.UrlEncode()
WebUtility.UrlEncode()
 来代替。

您选择的具体实施方式将取决于您的具体要求。


-7
投票
不要浪费你的时间,我对这些所谓的 url 编码器有很多经验,它们都是无用的,并且有不同的怪癖。例如 WebUtility.UrlEncode 不处理“+”号。

如果要对 URL 参数进行编码,请使用 BASE58 编码。它只使用字母+数字,因此不需要进行url编码。

© www.soinside.com 2019 - 2024. All rights reserved.