Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / Visual Basic
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Sep 2011CPOL 14.4K   2  
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:
C#
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:
VB.NET
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)


Written By
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 --