Why is the Url value in C# WebBrowser always null?

Viewed 386

I am trying to load a local HTML file into an instance of C# WebBrowser (WinForms). This is what I am doing:

string url  = @"file:///C:MyHtml/hello.html";
myWebbrowser.Url    = new Uri(url, UriKind.Absolute);
object test = myWebbrowser.Url; // breakpoint here

The path above is correct; if I copy it and paste into an external browser, the file is immediately opened. But the instance of WebBrowser does not want to react. I set a breakpoint in the last line of the snippet, and what I get there is that myWebbrowser.Url is null (the test variable). The control remains correspondingly empty.

myWebbrowser.AllowNavigation is explicitly set to true. I have also tried all possible versions of slashes and backslashes; the result is always the same. The version of the webbrowser seems to be 11 (myWebbrowser.Version = "{11.0.18362.1139}"). I am working in Windows 10, VS 2019.

What can be wrong in this setup?

2 Answers

The path above is correct; if I copy it and paste into an external browser, the file is immediately opened. But the instance of WebBrowser does not want to react. I set a breakpoint in the last line of the snippet, and what I get there is that myWebbrowser.Url is null.

I was able to replicate this exact issue, it's because the property of the Url doesn't get actually set until the document has actually finished loading.

To resolve this issue, you must handle the DocumentCompleted event. You can do so for example:

 string url  = @"file:///C:MyHtml/hello.html";
 myWebbrowser.DocumentCompleted += MyWebbrowser_DocumentCompleted;
 myWebbrowser.Url = new Uri(url, UriKind.Absolute);

Create a new routine to handle the DocumentCompleted event:

 private void MyWebbrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
 {
    string test = myWebbrowser.Url.ToString();
 }

You can also get the Url from the WebBrowserDocumentCompletedEventArgs:

 string testUrl = e.Url.ToString();

I am not sure exactly why when setting the Url and then checking it, it is null, I haven't found anything to explain why. My only guess is that it may be an invalid Url and or path, if navigation succeeds then that property is set.

Edit: upon looking at the source for WebBrowserDocumentCompleted, it does seem the Url property is only set in the DocumentCompleted, you can see more there.


Please note: you must register the DocumentCompleted event first before setting the Url property as when you do, it will navigate first and you will not receive the DocumentCompleted event.

I was able to find out why it did not want to function, at least I hope so. The "hello.html" file contained calls to jquery and THREE.js, while WebBroser seems not to support the latter. Therefore I did not see any content in the control. After I threw out THREE.js and inserted most simple HTML code, it worked just OK! Now I am busy trying to bring WebBrowser to support THREE.js (there exists a skeptical opinion about this, though).

Related