webview2

WebView2 not loading string HTML in MVC (source to Uri works)


Updating a lot of old IE11 Browser and CefSharp stuff to WebView2 - directing to a webpage (source) works, but loading a local string does not. This is in a MVC app.

  WebView = New WebView2
  With WebView
  .Name = "WebViewer"
  .EnsureCoreWebView2Async()
  .Source = New Uri("https://localhost:8081")
  End With



 AddHandler WebView.NavigationStarting, AddressOf ImageViewerStarting
 AddHandler WebView.NavigationCompleted, AddressOf ImageViewCompleted


Private Sub ImageViewerStarting(sender As Object, e As EventArgs)
Application.Current.Dispatcher.Invoke(Sub()

                                      WebView.NavigateToString(vHTML)
                                  End Sub)
   End Sub

The string it is loading (vHTML) is

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Viewer</title>
<link href="../../../resources/shadow2.css" rel="stylesheet"/>
<style>
</style>
</head>
<body>
<p>Content goes here</p>
</body)
</html>

It runs past 'starting' and gets to 'completed' - but nothing is loaded.

If I right click > Inspect and look at DevTools only html, head and body are showing.

Any ideas?

UPDATE

When debugging I noticed that NavigationStarting and NavigationCompleted keep looping with .NavigateToString but just do it twice with .Source

I set a variable counter to increment by one in the pop-up status bar every time NavigationCompleted was hit - one page is trying to load a simple html string and the other loads Google. As you can see the string one just keeps looping until the pop-up is closed - over 24k in a few seconds.

NavigateToString

Source


Solution

  • Fixed it by changing

    AddHandler WebView.NavigationStarting, AddressOf ImageViewerStarting
    

    to

    AddHandler WebView.CoreWebView2InitializationCompleted, AddressOf ImageViewerStarting