Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is HttpUtility.UrlPathEncode marked as "do not use"?

Why does the documentation of .NET for HttpUtility.UrlPathEncode for .NET 4.5 state

Do not use; intended only for browser compatibility. Use UrlEncode.

UrlEncode does not do the same, it encodes a string for the parameter part of a URL, not for the path part. Is there a better way to encode a string for the path part and why shouldn't I use this function, which is in the framework since 1.1 and works?

like image 364
Dehalion Avatar asked Nov 20 '13 10:11

Dehalion


1 Answers

based on MSDN, they recommend to use UrlEncode to grantee that it is working for all platforms and browsers

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.

Also UrlEncode is using UTF-8 encoding so if you are sending query string in different language like Arabic you should use UrlEncode

like image 156
Hussein Khalil Avatar answered Sep 29 '22 01:09

Hussein Khalil