WP7 Navigation Gotcha

This afternoon I discovered a bug in one of the apps I am working on. It happens because the keys we use for navigation are provided in a third-party feed, and one of those keys contains a plus sign (“+”).

This wasn’t immediately apparent as an issue, and we just navigated using

NavigationService.Navigate(new Uri("/Views/TargetView.xaml?key=" + myKeyValue, UriKind.Relative));

Unfortunately, this gets UrlDecoded on the receiving side as a sapce, so our key that started out being “text+text” was being resolved as “text text”. When looking that value up in our tables the key was obviosuly not found and a NullReferenceException was thrown.

The fix was pretty simple, though. We just had to UrlEncode the strong before we sent it, as follows.

NavigationService.Navigate(new Uri("/Views/TargetView.xaml?key=" + System.Net.HttpUtility.UrlEncode(myKeyValue), UriKind.Relative));

and all is good with the world again.