0

I am using WebView2 within Wpf to render html documents. These html documents can contain a elements which I need to open in the default browser. In order to do this, I intercept the webView.CoreWebView2.NavigationStarting event and open the url in the default browser.

When I try to fetch the Uri from the webView.CoreWebView2.NavigationStarting event, the url is correctly returned for https urls but not http urls. Http urls will have their uri returned as "about:blank#blocked".

 private void CoreWebView2_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
 {
     try
     {
         string uri = e.Uri.ToString();

         if (uri.StartsWith("https://") || uri.StartsWith("http://"))
         {
             Process.Start(new ProcessStartInfo(uri) { UseShellExecute = true });
             e.Cancel = true;
         }
     }
     catch (Exception ex)
     {
         webView.Source = new Uri(e.Uri.ToString());
         Log.AddException("Unable to open hyperlink in default browser due to error:  " + ex.ToString());
     }
 }

Any help would be much appreciated. Thanks everyone!

I've had a go at disabling web security like below but with no avail:

CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions("disable-web-security");
    CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(null, null, options);
    await webView21.EnsureCoreWebView2Async(environment);

I also thought to look in the event's request headers to try and fish out the url but again with no luck.

1
  • 2
    Can you verify that the default browser on that machine for that user will load an unsecure web-page in the first place? There's plenty of configuration spots on a windows machine that would cause this which have absolutely nothing to do with your code. Enterprise level configurations would do it. I'm pretty sure I can't hit an http:// site from my office network, ever. Commented Oct 10, 2023 at 16:11

1 Answer 1

0

The issue was a malformed href with ” instead of ".

<a href=”http://www.testingmcafeesites.com/”>HTTP Link</a>

The whole html document I was using for testing is below:

<!DOCTYPE html>
<html>
<body>

<h2>Test</h2>
<div>
    <p>No target attribute: </p>
    <a href="https://www.google.com/">Google</a>
    <a href="https://www.bbc.com/">BBC News</a><br/>
    <a href=”http://www.testingmcafeesites.com/”>HTTP Link</a>

    <p>Target attribute: </p>
    <a href="https://www.google.com/" target="_blank">Google with target attribute: blank</a><br/>
    <a href="https://www.bbc.com/" target="_parent">BBC News with target attribute: parent</a><br/>
    <a href="https://www.bbc.com/" target="_top">BBC News with target attribute: top</a><br/>

    <p>Relative link: </p>
    <a href="/css/default.asp">CSS Tutorial</a>
    <a href="/css/default.asp">CSS Tutorial</a>
    <a href="/css/default.asp">CSS Tutorial</a>
    <a href="/css/default.asp">CSS Tutorial</a>
</div>
</body>
</html>

Upon changing to the correct double quote character the HTTP link works.

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.