Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.UrlEncode(string s)... doesn't

Server.UrlEncode("My File.doc") returns "My+File.doc", whereas the javascript escape("My File.doc") returns "My%20File.doc". As far as i understand it the javascript is corectly URL encoding the string whereas the .net method is not. It certainly seems to work that way in practice putting http://somesite/My+File.doc will not fetch "My File.doc" in any case i could test using firefox/i.e. and IIS, whereas http://somesite/My%20File.doc works fine. Am i missing something or does Server.UrlEncode simply not work properly?

like image 508
Ben Robinson Avatar asked Dec 05 '22 03:12

Ben Robinson


1 Answers

Use Javascripts encodeURIComponent()/decodeURIComponent() for "round-trip" encoding with .Net's URLEncode/URLDecode.

EDIT

As far as I know, historically the "+" has been used in URL encoding as a special substitution for the space char ( ASCII 20 ). If an implementation does not take the space into consideration as a special character with the '+' substitution, then it still has to escape it using its ASCII code ( hence '%20' ).

There is a really good discussion of the situation at http://bytes.com/topic/php/answers/5624-urlencode-vs-rawurlencode. It's inconclusive, by the way. RFC 2396 lumps the space with other characters without an unreserved representation, which sides with the '%20' crowd.

RFC 1630 sides with the '+' crowd ( via forum discusion )...

Within the query string, the plus sign is reserved as shorthand notation for a space. Therefore, real plus signs must beencoded. This method was used to make query URIs easier to pass in systems which did not allow spaces.

Also, the core RFCs are...

RFC 1630 - Universal Resource Identifiers in WWW

RFC 1738 - Uniform Resource Locators (URL)

RFC 2396 - Uniform Resource Identifiers (URI): Generic Syntax

like image 122
kervin Avatar answered Dec 09 '22 15:12

kervin