Click here to Skip to main content
Click here to Skip to main content

Navigate a Silverlight WebBrowser to a URL with a Hash (Fragment Identifier)

, 26 Sep 2011
Rate this:
Please Sign up or sign in to vote.
The Silverlight WebBrowser won't navigate to some URL's, but this can be worked around.
If you try to set the Silverlight WebBrowser control to navigate to http://translate.google.com/#en|fr| either via the Source property in the XAML or by using the Navigate function, you will be presented with an exception (strangely, the Windows Forms WebBrowser has no problem with this URL). It seems the Uri class doesn't like to construct URLs that contain a pipe character in the hash (aka, fragment identifier) of the URL (the part of the URL that comes after the "#"). Here is the workaround:
string html = string.Format(
  @"<html><body><script type='text/javascript'>window.location = '{0}';</script></body></html>",
  "http://translate.google.com/#en|fr|");
myWebBrowser.NavigateToString(html);
All you have to do is navigate to some HTML that has JavaScript that navigates to the URL you want the WebBrowser to visit. Here's the VB.NET version:
Dim html As String = String.Format(
  "<html><body><script type='text/javascript'>window.location = '{0}';</script></body></html>",
  "http://translate.google.com/#en|fr|")
myWebBrowser.NavigateToString(html)
I suspect this is either a .NET bug in the implementation of Uri or pipe characters may not strictly be allowed in fragment identifiers. Whatever the reason, this is the workaround (though if somebody else has a more elegant solution, I would love to see it).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

AspDotNetDev
Web Developer
United States United States
  • Managing Your JavaScript Library in ASP.NET (if you work with ASP.net and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 26 Sep 2011
Article Copyright 2011 by AspDotNetDev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid