System.Web.HttpUtility.UrlEncode/UrlDecode ASP.NET 5的替换

bez*_*eny 68 c# asp.net asp.net-mvc asp.net-core-mvc asp.net-core

我想知道是否有替换System.Web.HttpUtility.UrlEncodeUrlDecode.

正如我发现Encode它应该是: Microsoft.Framework.WebEncoders.UrlEncoder.Default.UrlEncode.

但我没有找到UrlDecode.有吗?

Mih*_*scu 119

System.Runtime.Extensions定义了UrlDecodeHtmlDecode.

namespace System.Net
{
    public static partial class WebUtility
    {
        public static string HtmlDecode(string value) { return default(string); }
        public static string HtmlEncode(string value) { return default(string); }
        public static string UrlDecode(string encodedValue) { return default(string); }
        public static byte[] UrlDecodeToBytes(byte[] encodedValue, int offset, int count) { return default(byte[]); }
        public static string UrlEncode(string value) { return default(string); }
        public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count) { return default(byte[]); }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新

System.Runtime.Extensions定义扩展时,您可以从它的代码中注意到您需要调用的实际类System.Net.WebUtility

选项1:System.Net.WebUtility

目前,还没有公开作出的计划,包括DecodeMicrosoft.Framework.WebEncoders.

用法

System.Net.WebUtility.UrlEncode(myString)
System.Net.WebUtility.UrlDecode(myString)
Run Code Online (Sandbox Code Playgroud)

选项2:System.Text.Encodings.Web.UrlEncoder

这是在asp.net核心服务容器中注册的,可以注入您的控制器等.

  • 所以调用的是System.Net.WebUtility.HtmlDecode(stringToDecode).我添加了这个评论,因为我首先混淆了添加System.Runtime.Extensions而不是这种情况,也许它可以帮助其他人. (6认同)
  • 非常感谢你的回答.它有效,但我的问题很小.我正在尝试编码一个值,我将结果与我之前在.NET 4.5中的应用程序进行比较.结果有点不同.在旧的asp.net中,我得到了编码值结果:**kA5viZqq23%2f4LCe4IboZ7Q%3d%3d**和新的UrlEncode重新使用的是:**kA5viZqq23%2F4LCe4IboZ7Q%3D%3D**差别很少字母是资本.因此,像**%3D**这样的一些值在我的旧版本中是等号,而在新版本中,它只写为**%3D**.你对此有什么想法吗? (2认同)