HttpServerUtility.UrlPathEncode vs HttpServerUtility.UrlEncode

What's the difference between HttpServerUtility.UrlPathEncode and HttpServerUtility.UrlEncode? And when should I choose one over the other?


Update: as of 4.5, per MSDN reference, Microsoft recommends to only use UrlEncode. Also, the information previously listed in MSDN does not fully describe behavior of the two methods - see comments.

The difference is all in the space escaping - UrlEncode escapes them into + sign, UrlPathEncode escapes into %20. + and %20 are only equivalent if they are part of QueryString portion per W3C. So you can't escape whole URL using + sign, only querystring portion. Bottom line is that UrlPathEncode is always better imho

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.

http://msdn.microsoft.com/en-us/library/4fkewx0t.aspx

UrlPathEncode vs. UrlEncode, UrlPathEncode and HttpServerUtility.UrlEncode? And when should I choose In this url, foo, fooval, bar, and barval should EACH be UrlEncode'd separately:� asp.net - HttpServerUtility.UrlPathEncode vs HttpServerUtility.UrlEncode (2) UrlEncode is useful for query string values (so to the left or especially, right, of each =).


UrlEncode is useful for query string values (so to the left or especially, right, of each =).

In this url, foo, fooval, bar, and barval should EACH be UrlEncode'd separately:

http://www.example.com/whatever?foo=fooval&bar=barval

UrlEncode encodes everything, such as ?, &, =, and /, accented or other non-ASCII characters, etc, into %-style encoding, except space which it encodes as a +. This is form-style encoding, and is best for something you intend to put in the querystring (or maybe between two slashes in a url) as a parameter without it getting all jiggy with the url's control characters (like &). Otherwise an unfortunately placed & or = in a user's form input or db value value could break things.

EDIT: Uri.EscapeDataString is a very close match to UrlEncode, and may be preferable, though I don't know the exact differences.

UrlPathEncode is useful for the rest of the query string, it affects everything to the left of the ?.

In this url, the entire url (from http to barval) should be run through UrlPathEncode.

http://www.example.com/whatever?foo=fooval&bar=barval

UrlPathEncode does NOT encode ?, &, =, or /. It DOES, however, like UrlEncode, encode accented/non-ASCII characters with % notation, and space also becomes %20. This is useful to make sure the url is valid, since spaces and accented characters are not. It won't touch your querystring (everything to the right of ?), so you have to encode that with UrlEncode, above.

In ASP.NET, why is there UrlEncode() AND UrlPathEncode()?, asp.net HttpServerUtility.UrlPathEncode vs HttpServerUtility.UrlEncode? Also UrlPathEncode doesn't encode anything after the first occurence of "?". It assumes� asp.net - HttpServerUtility.UrlPathEncode 대 HttpServerUtility.UrlEncode (2) 업데이트 : MSDN 참조 4.5에 따라 Microsoft는 UrlEncode 만 사용하는 것이 좋습니다.


To explain it as simply as possible:

HttpUtility.UrlPathEncode("http://www.foo.com/a b/?eggs=ham&bacon=1") 

becomes

http://www.foo.com/a%20b/?eggs=ham&bacon=1

and

HttpUtility.UrlEncode("http://www.foo.com/a b/?eggs=ham&bacon=1") 

becomes

http%3a%2f%2fwww.foo.com%2fa+b%2f%3feggs%3dham%26bacon%3d1

asp.net HttpServerUtility.UrlPathEncode vs HttpServerUtility , To encode or decode values outside of a web application, use the WebUtility class. UrlEncode(String). URL-encodes a string and returns the encoded string. Stampa la penultima colonna / campo in awk Cercando di triggersre l’evento onload sul tag script HttpServerUtility.UrlPathEncode vs HttpServerUtility.UrlEncode React JS – Uncaught TypeError: this.props.data.map non è una funzione Come viene chiamato questo modello JavaScript e perché viene utilizzato?


HttpServerUtility.UrlEncode Method (System.Web), To encode or decode values outside of a web application, use the class. can encode a URL using with the UrlEncode method or the UrlPathEncode method. HttpServerUtility.UrlPathEncode vs HttpServerUtility.UrlEncode; Imansible eseguire il cast dell'object di tipo 'System.DBNull' per digitare 'System.String`


HttpUtility.UrlEncode Method (System.Web), What's the difference between HttpServerUtility.UrlPathEncode and HttpServerUtility.UrlEncode? And when should I choose one over the other? Valutare le espressioni aritmetiche dalla stringa in C ++ HttpServerUtility.UrlPathEncode vs HttpServerUtility.UrlEncode Le enumerazioni possono essere sottoclass per aggiungere nuovi elementi? Sostituzione di fflush (stdin)


HttpServerUtility.UrlPathEncode vs HttpServerUtility , When we are dealing with URL encode in .NET Core 2.1, there are two APIs: System.Net.WebUtility.UrlEncode and System.Web.HttpUtility. Ignorare una proprietà di class in Entity Framework 4.1 Code First HttpServerUtility.UrlPathEncode vs HttpServerUtility.UrlEncode Cambia l’ordine di una scala x discreta In che modo Git gestirà una collisione SHA-1 su un BLOB? estensione chrome: come inviare l’evento ‘keydown’ all’input della pagina?