Using querystring parameters with the WebBrowserTask on WP7

I have just built a Windows Phone 7 app that is hopefully about to pop up on the marketplace within a couple of days. It is a fairly simple app, with very few impressive features. The most complicated part was to get the SQLite db used by the iPhone version of the app, to work on Windows Phone 7. Unfortunately I did not get SQLite to work on WP7. I ended up transferring the data to XML and adding some index tables, also in XML. But that isn’t the topic for this post.

The reason that I am writing this, is that I got stuck on a little thing while doing. Nothing complicated, but worth noting…

A part of the functionality of this application is to launch a web browser and redirect it to a predefined address, which is obviously a task (no pun intended…) for the WebBrowserTask. But the thing is that the URL that was being used, was using querystring parameters, and some of them didn’t “come through” and thus sent the browser off to a location without enough parameters. It seemed as though the first parameter would go through, but any subsequent parameter would just be chopped off. So I thought I might have to use HttpUtility.UrlEncode() somehow. But that didn’t work either.

But after having a quick look on the MSDN site for the usage of the WebBrowserTask, it was pretty obvious what was needed. Any URL used by the WebBrowserTask should be passed through the static method Uri.EscapeUriString().

This call will make sure to format it properly and get any parameters into the WebBrowserTask as needed. It is only necessary if the URL contains “funny” characters. Just a plain web address will not cause problems. But as soon as you start adding querystring parameters, you are in a world of hurt if you do not use Uri.EscapeUriString().

That’s all for now! It’s time to get back to my vacation and enjoy Fiji to its fullest…

Comments are closed