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.