The regular .Net framework contains HttpUtility.UrlEncode in the System.Web Assembly and in Silverlight it appears it was moved to System.Windows.Browser. But in Windows Phone 7 (which I thought was the same as Silverlight) I can't seem to find a proper way to UrlEncode anything. Neither of the previously mentioned assemblies are available in the Windows Phone 7 environment.
The HttpUtility. UrlEncode method uses UTF-8 encoding by default. Therefore, using the UrlEncode method provides the same results as using the UrlEncode method and specifying UTF8 as the second parameter. UrlEncode is a convenient way to access the UrlEncode method at run time from an ASP.NET application.
HttpUtility.UrlEncode Method (System.Web)Encodes a URL string. These method overloads can be used to encode the entire URL, including query-string values. To encode or decode values outside of a web application, use the WebUtility class.
HttpUtility.UrlDecode Method (System.Web)Converts a string that has been encoded for transmission in a URL into a decoded string. To encode or decode values outside of a web application, use the WebUtility class.
Silverlight has the Uri.EscapeUriString and Uri.EscapeDataString which can be used to URL-encode portions of a URI such as query strings and path components and such.
Even in the desktop framework, you should never take a dependency on System.Web.dll just for HttpUtility, although many developers still do it. This is bad for 3 reasons.
In addition to the Uri escaping methods described above, if you need HtmlEncode, I would just rip code off that someone else wrote and drop it in your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With