3

C# 等效于encodeURIComponent在 SO 和其他地方都有很好的介绍,但是encodeURI呢?基本上我只想编码无效的URL 字符而不是保留字符,例如/,:等。所以

"http://www.example.com/my cool page"

将被编码为

"http://www.example.com/my%20cool%20page"

.NET 中有什么东西可以做到这一点吗?还是正则表达式是我最好的选择?

4

4 回答 4

6

尝试

Uri.EscapeUriString("http://www.mysite.com/my cool page")
于 2014-02-10T20:34:56.803 回答
1

尝试

Server.URLEncode(uri.ToString)
于 2014-02-10T20:34:45.457 回答
1

试试这个:

HttpUtility.UrlEncode(String)

例如:

var url_encoded_string = HttpUtility.UrlEncode(userInput);
于 2014-02-10T20:36:59.373 回答
0

你可以使用System.Net.WebUtility.UrlEncode(string value).

于 2014-02-10T20:46:01.497 回答