none
hyperlink button cannot navigate to web page within same solution

    Question

  • I have a Silverlight solution called Jaba, with 2 projects in it - Jaba, and Jaba.Web.  Under Jaba, I have a file MainPage.xaml.  Inside that page there is a frame, inside the frame is a grid, and inside the grid are my controls. 

    One of the controls is a hyperlink button.  I want that button to display an html file that I put in the same directory as the MainPage.xaml file, that is, under the Jaba project directory.  The html file is called HelpHowTo.htm  I have the build action on that file set to Resource. 

    The target of the hyperlink button is _blank, because I want the html file to show up in a new tab in the browser. 

    I've tried a lot of things for the NavigateUri property, including HelpHowTo.htm, /HelpHowTo.htm, ..//HelpHowTo.htm, ...//Jaba/HelpHowTo.htm, and lots of other variations of the above.  I keep getting the error:

    An unhandled exception ('Unhandled Error in Silverlight Application Code: 4004 Category: ManagedRuntimeError Message: System.NotSupportedException: Cannot navigate to locations relative to a page.

    If I set the NavigateUri property to http://www.msdn.com it works fine.  I can display someone else's web page, but not a web page within my own application!  What am I doing wrong and how do I fix it?  I can move the html file somewhere else in my solution if that would help.

    I'm using Silverlight 4 under VS2010, with C#.

    Thanks.

    Monday, May 02, 2011 2:34 PM

Answers

  • Because that is not valid URL. That's the file path which dosen't work in this case.

    Change your code to the following:

    Uri uri = new Uri("../HelpHowTo.html", UriKind.Relative);

    Make sure to set your Web project as Start up project (not the Silverlight project). Your Page URL should be something like:

    http://XXX/YourPage.html



    Tuesday, May 03, 2011 6:03 PM

All replies

  • Some further information.  I tried changing the relative path into an absolute uri.  First I copied the html file to the bin/Debug directory under Jaba project.  Then I used the following code to get the absolute uri and navigating to it:

    using System.Windows.Browser

    Uri myAbsoluteUri = new Uri(HtmlPage.Document.DocumentUri, "./HelpHowTo.htm");

    HtmlPage.Window.Navigate(myAbsoluteUri, "_blank");

    I got the error: InvalidOperationException was unhandled by user code  Access is denied.

    How are we supposed to display web pages that are inside our application?  This must be something that people need to do all the time.

    Monday, May 02, 2011 3:55 PM
  • If you want to navigate to other Html (or aspx) pages, make sure to put the html page under your Web project, not your Silverlight project.


    Monday, May 02, 2011 4:03 PM
  • I put a copy of it there but I still get the same error message.

    Monday, May 02, 2011 4:30 PM
  • Test your URL first. Type the URL in a browser, do you see your html page?

    Monday, May 02, 2011 7:23 PM
  • Yes I do.

    Tuesday, May 03, 2011 10:09 AM
  • Your mean when you put the exact same URL in this function and it doesn't work?

    HtmlPage.Window.Navigate(YourPageURL, "_blank");



    Tuesday, May 03, 2011 10:17 AM
  • If I type the following uri directly into a browser window it works.

    file:///C:/Elisa/jaba/TFS/JABA/JABA/HelpHowTo.htm

    But if I use the following two lines of code:

    Uri myAbsoluteAbsoluteUri = new Uri("file:///C:/Elisa/jaba/TFS/JABA/JABA/HelpHowTo.htm", UriKind.Absolute);

    HtmlPage.Window.Navigate(myAbsoluteAbsoluteUri, "_blank");

    I get the error "access denied"

    Tuesday, May 03, 2011 4:37 PM
  • Because that is not valid URL. That's the file path which dosen't work in this case.

    Change your code to the following:

    Uri uri = new Uri("../HelpHowTo.html", UriKind.Relative);

    Make sure to set your Web project as Start up project (not the Silverlight project). Your Page URL should be something like:

    http://XXX/YourPage.html



    Tuesday, May 03, 2011 6:03 PM
  • Thank you!!!  Bottom line is it wasn't working because the solution start page was set to the silverlight app instead of the web app.

    Wednesday, May 04, 2011 10:36 AM