C#'s equivalent to encodeURIComponent is well-covered on SO and elsewhere, but what about encodeURI? Basically I want to encode invalid URL characters only and not reserved characters such as /, :, etc. So

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

would be encoded to

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

Is there something baked into .NET to do this? Or is a regex my best bet?

有帮助吗?

解决方案

Try

Uri.EscapeUriString("http://www.mysite.com/my cool page")

其他提示

Try

Server.URLEncode(uri.ToString)

Try this:

HttpUtility.UrlEncode(String)

For example:

var url_encoded_string = HttpUtility.UrlEncode(userInput);

You could use System.Net.WebUtility.UrlEncode(string value).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top