首页 / 问答 / [.NET/.NET Core]ASP.NET Core应用程序中如何对URL地址进行编码(Encode)或者解码(Decode)呢?

[.NET/.NET Core]ASP.NET Core应用程序中如何对URL地址进行编码(Encode)或者解码(Decode)呢?

.NET C# .NET Core ASP.NET Core .NET 5 2.14K 次浏览
0

在.NET Framework的应用程序编程开发中,我们可以使用HttpContext.Current.Server.UrlEncode对给定的URL地址进行编码,但在ASP.NET Core应用程序编程开发中这个方法不可用了,如果要对URL进行编码或者解码,应该如何实现呢?

回复 [×]
提交评论
请输入评论内容

1 个回答

  • 0

    在ASP.NET Core 2.0及以上版本中,安装System.Runtime.Extension程序包,位于System.Net命名空间下的WebUtility类中提供了URL编码和解码的方法,内置的方法如下:

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

    使用示例如下:

    var encodeUrl = System.Net.WebUtility.UrlEncode(url);
    
    Rector的个人主页

    Rector

    2021-04-02 回答

    我来回答