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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.