C# 等效于encodeURIComponent在 SO 和其他地方都有很好的介绍,但是encodeURI呢?基本上我只想编码无效的URL 字符而不是保留字符,例如/
,:
等。所以
"http://www.example.com/my cool page"
将被编码为
"http://www.example.com/my%20cool%20page"
.NET 中有什么东西可以做到这一点吗?还是正则表达式是我最好的选择?
尝试
Uri.EscapeUriString("http://www.mysite.com/my cool page")
尝试
Server.URLEncode(uri.ToString)
试试这个:
HttpUtility.UrlEncode(String)
例如:
var url_encoded_string = HttpUtility.UrlEncode(userInput);
你可以使用System.Net.WebUtility.UrlEncode(string value)
.