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?

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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).
2

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

1 Comment

It is hard to know what he expects. He just notes it changed, not which version of it he wants.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.