wtf*_*512 92 c# asp.net-core-mvc asp.net-core
HttpContext.Current.Server.UrlEncode
Run Code Online (Sandbox Code Playgroud)
它仅适用于.NET Framework.如何在ASP.NET Core项目中编码或解码uri参数?
Set*_*Set 152
对于ASP.NET Core 2.0+,只需添加System.Net命名空间 - WebUtility类作为System.Runtime.Extensionsnuget包的一部分提供,默认情况下在ASP.NET Core项目中引用.
对于以前的版本,添加Microsoft.AspNetCore.WebUtilitiesnuget包.
然后WebUtility课程将为您提供:
public static class WebUtility
{
public static string UrlDecode(string encodedValue);
public static string UrlEncode(string value);
}
Run Code Online (Sandbox Code Playgroud)
Man*_*ves 75
它可以在.Net Core SDK的2.0.0版本中找到System.Net.WebUtility.UrlEncode(参见文档)
ttu*_*tes 32
对于ASP.Net Core 2.0+以及如果需要空格来编码为 %20
而不是+;
使用:
Uri.EscapeDataString(someString);
Run Code Online (Sandbox Code Playgroud)
Mic*_*ael 15
只需添加另一种方法即可:
UrlEncoder.Default.Encode(url);
Run Code Online (Sandbox Code Playgroud)
https://learn.microsoft.com/en-us/dotnet/api/system.text.encodings.web.urlencoder?view=net-6.0
我正在使用重定向,而 UrlEncode 对我不起作用,因为它对整个网址进行编码。我通过使用 UriHelper.Encode 解决了这个问题,如下所示。
// generate url string...
return Redirect(Microsoft.AspNetCore.Http.Extensions.UriHelper.Encode(new System.Uri(url)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54731 次 |
| 最近记录: |