0

I'm trying to include js and css resource files that are local to my project on an html page loaded as a string into a WPF WebBrowser control.

The project builds a dll for a desktop application. Using Visual Studio 2013 and C#.

The HTML loads in the the WebBrowser, but I get errors related to including the JS file.

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string page = GetPage();
        if (page != null)
        {
            string resourcePath = System.IO.Path.GetFullPath("Resources/");

            //  Try setting an absolute path
            page = page.Replace("Resources/", resourcePath);
            this.webbrowser.NavigateToString(page);
        }
    }

Embedded within the html string is the following:

<head>
    <script language="JavaScript" src="Resources/myscript.js"></script> 
    <link rel="stylesheet" type="text/css" href="Resources/mystyle.css"> 
</head>

I get an error popup like:

An error has occurred in the script on this page.

URL:  aboutC:\MyDir\MyProject\bin\Debug\Resources/myscript.js

It prepends "about" to the URL. I probably also need to change the slashes. I tried a number of things. "about" is always prepended.

Also tried using IsolatedStorage, but that doesn't work for my project: http://msdn.microsoft.com/library/windows/apps/ff431811(v=vs.105).aspx

John Saunders
  • 160,644
  • 26
  • 247
  • 397
George Hernando
  • 2,550
  • 7
  • 41
  • 61
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 26 '14 at 22:56

1 Answers1

4

You have to use absolute URIs when calling NavigateToString. Since you are not, IE assumes about: as the relative path to cause it to fail.

Either use file:///C:\some\path.css, res://somedll.dll/path.css, or host a HTTP server using WCF on a random high port and use http://localhost:37458/path.css.

Community
  • 1
  • 1
Mitch
  • 21,223
  • 6
  • 63
  • 86