7

In a recent project, I had the pleasure of troubleshooting a bug that involved images not loading when spaces were in the filename. I thought "What a simple issue, I'll UrlEncode() it!" But, NAY! Simply using UrlEncode() didn't resolve the problem.

The new problem was the HttpUtilities.UrlEncode() method switched spaces () to plusses (+) instead of %20 like the browser wanted. So file+image+name.jpg would return not-found while file%20image%20name.jpg was found correctly.

Thankfully, a coworker pointed out HttpUtilities.UrlPathEncode() to me which uses %20 for spaces instead of +.

WHY are there two ways of handling Url encoding? WHY are there two commands that behave so differently?

quakkels
  • 11,676
  • 24
  • 92
  • 149
  • See http://stackoverflow.com/questions/602642/server-urlencode-vs-httputility-urlencode/603962#603962 for a better solution than either. – Jon Adams Nov 03 '11 at 16:00
  • Possible duplicate: http://stackoverflow.com/q/4145823/2291 – Jon Adams Nov 03 '11 at 16:02
  • @Mufasa - mmm... I know the difference. I'm wondering why. – quakkels Nov 03 '11 at 18:52
  • The answer http://stackoverflow.com/questions/4145823/httpserverutility-urlpathencode-vs-httpserverutility-urlencode/4145961#4145961 in that duplicate question sort of answers why. But the better solution makes most of this discussion not too important. Libraries improve over time and MS is very rigid with backwards compatibility so they add new methods instead of 'fixing' old ones. – Jon Adams Nov 03 '11 at 19:15

2 Answers2

9

UrlEncode is useful for use with a QueryString as browsers tend to use a + here in place of a space when submitting forms with the GET method.

UrlPathEncode simply replaces all characters that cannot be used within a URL, such as <, > and .

Both MSDN links include this quote:

You can encode a URL using with the UrlEncode method or the UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.

Connell
  • 13,925
  • 11
  • 59
  • 92
  • 1
    Microsoft have since stated that `UrlPathEncode()` should not be used. It's a helper method used to ensure browser compatibility. See your UrlPathEncode link for details. – Basic Feb 28 '15 at 01:35
  • 1
    UrlEncode could not be used to encode parts of path, because + sign is not allowed there. See explanation: https://stackoverflow.com/a/29948396/991267 System.Uri.EscapeDataString method may be used to encode path parts. – Der_Meister Jul 11 '17 at 04:44
2

So in a URL you have the path and then a ? and then the parameters (i.e. http://some_path/page.aspx?parameters). URL paths encode spaces differently then the url parameters, that's why there is the two versions. For a long time spaces were not valid in a URL, but were in in the parameters.

In other words the formatting urls has changed over time. For a long time only ANSI chars could be in a URL too.

James Becwar
  • 1,176
  • 2
  • 11
  • 20