I'm having trouble finding the correct way to URL encode a string in C#. What I want is to encode the string some string to some%20code. Using HttpUtility.URLEncode(); method encode is to some+string.
3 Answers
HttpUtility.UrlEncode does the right thing here.
Encodes a URL string. The UrlEncode method can be used to encode the entire URL, including query-string values.
When it comes to spaces on the URL, a + or %20 are both correct.
2 Comments
Gregor Menih
The problem was that I was sending a HTTP request to get the web page, where '+' was not valid. Eg. Sending a query with 'some+string' would search for 'some+string', not 'some string'.
Peter Mortensen
What is the minimum required .NET version for HttpUtility.UrlEncode() (not necessarily what is listed in the MSDN page)?
This thread includes a discussion of some of the in-built encoding options for URIs:
string.Replace("+", "%20")?