Dan Sewell
Dan Sewell

Reputation: 1290

C# URL QueryString Trouble

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?

Upvotes: 1

Views: 711

Answers (3)

Jon Hanna
Jon Hanna

Reputation: 113232

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);

Upvotes: 2

Jared Peless
Jared Peless

Reputation: 1120

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

Upvotes: 2

Alan
Alan

Reputation: 46813

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.

Upvotes: 2

Related Questions