1

I have a WP7 project where I am using the below code. It normally works ok, but I am getting a strange result with some particular strings being passed through.

Service = "3q%23L3t41tGfXQDTaZMbn%23w%3D%3D?f"

NavigationService.Navigate(new Uri("/Details.xaml?service=" + Service, UriKind.Relative));

Next Page:

NavigationContext.QueryString.TryGetValue("service", out Service1);

Service1 now = 3q#L3t41tGfXQDTaZMbn#w==?f

Why has the string changed?

Dan Sewell
  • 1,278
  • 4
  • 18
  • 45

3 Answers3

2

In your first code snippet the string is URL Encoded.

In the 2nd code snippet, the string is URL Decoded.

They are essentially the same strings, just with encoding applied/removed.

For example: urlencoding # you get %23

For further reading check out this wikipedia article on encoding.

Since HttpUtility isn't part of WP7 Silverlight stack, I'd recommend using Uri.EscapeUriString to escape any URI's that have not been escaped.

Alan
  • 45,915
  • 17
  • 113
  • 134
  • 1
    `Uri.EscapeUriString` would not be appropriate, they'd want `Uri.EscapeDataString` for this. The former just escapes characters that cannot appear in a URI, the latter escapes characters with a special meaning in URIs too, so that's what they want here. (In fairness, the MSDN documentation is not remotely clear on what the difference is). – Jon Hanna Jan 02 '12 at 22:15
2

You should probably URL encode the string if you want it to pass through unscathed.

Jared Peless
  • 1,120
  • 9
  • 11
2

The string hasn't changed, but you're looking at it in two different ways.

The way to encode 3q#L3t41tGfXQDTaZMbn#w==?f for as URI content is as 3q%23L3t41tGfXQDTaZMbn%23w%3D%3D?f. (Actually, it's 3q%23L3t41tGfXQDTaZMbn%23w%3D%3D%3Ff but you get away with the ? near the end not being properly escaped to %3F in this context).

Your means of writing the string, expects to receive it escaped.

Your means of reading the string, returns it unescaped.

Things are working pretty much perfectly, really.

When you need to write the string again, then just escape it again:

Service = Uri.EscapeDataString(Service1);
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251