0

I have a C# Winforms application that uses the WebView2 browser. The form contains a grid and a browser view. Whenever a user changes the selected row in the grid, new data is fetched, and an HTML file is generated based on that data. The WebView2 browser then navigates to the new file URL to display the updated content.

To handle cases where data fetching and file generation introduce a delay (which sometimes caused the browser to show stale data momentarily), I added an intermediate navigation to about:blank before navigating to the new file URL. So for each row change, two navigations happen:

  1. Navigate to about:blank
  2. Navigate to the generated HTML file

This approach worked fine for the past two years. However, in some recent setups, we started observing that both navigations (to about:blank and the file URL) happen almost simultaneously. As a result, the navigation to the actual file URL gets aborted, while navigation to about:blank succeeds.

We confirmed this using the NavigationStarting and NavigationCompleted events, and also tracked the navigation IDs.

private void NavigateToBlankThenFileAsync()
{
    webView2.CoreWebView2.Navigate("about:blank");
    var fileUrl = GetHTMLDataAndWriteItIntoFile();
    webView2.CoreWebView2.Navigate(fileUrl);
}

To mitigate the issue, I modified the logic to make the about:blank navigation synchronous — waiting for the navigation to complete before triggering the actual file navigation.

private TaskCompletionSource<bool> navigationCompletedTask;

// Call this function when you want to trigger navigation
private async Task NavigateToBlankThenFileAsync()
{
    navigationCompletedTask = new TaskCompletionSource<bool>();

    // Navigate to about:blank
    webView2.CoreWebView2.Navigate("about:blank");

    // Await navigation to about:blank before proceeding
    await navigationCompletedTask.Task;
var fileUrl = GetHTMLDataAndWriteItIntoFile();
    // Now safe to navigate to actual file URL
    webView2.CoreWebView2.Navigate(fileUrl);
}

// Event handler for navigation completed
private void WebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
    var uri = webView2.Source?.ToString();

    if (uri == "about:blank" && navigationCompletedTask != null)
    {
        navigationCompletedTask.SetResult(true);
        navigationCompletedTask = null;
    }
}

Question: is this the right approach to ensure reliable sequential navigation in WebView2?

Or is there a better or more robust way to prevent the second navigation (to the actual file) from being aborted?

Any known changes or configurations in recent environments or WebView2 updates that might explain this behavior?

Any suggestions or feedback on this would be appreciated

8
  • Any known changes or configurations in recent environments or WebView2 updates that might explain this behavior?: Read the WebView2 Release Notes or report the issue on Github. Commented Apr 5 at 17:26
  • 1
    Not clear how the code presented here solves the issue described: a delay introduced by the time needed to generate a data file. Do you mean that the path-to-generated-file.html may be storing old data, and the navigation to about:blank is used to counter-act the delay? If that's the case, IMO you should present a page that shows, say, an overlay that says Loading data..., then post a message to another JavaScript function (or inject / call one) in the file that sets the new location, when the data file is ready. Otherwise, you should probably explain a little better the overall issue Commented Apr 5 at 20:03
  • To make it simple have not added the file generation code. Let’s say you have the file already generated and you are doing navigation to the about blank first and to actual url second. With This solution - about call becomes synchronous as it awaits on task to be completed. Once about blank is successful it navigates to file url. Previously it was asynchronous both may get triggered at almost same time in certain scenario Commented Apr 6 at 8:03
  • "intermediate navigation to about:blank" could also be a page showing "Loading data..." ?? But the better solution would be to await the finish of the generated HTML file. (and when that takes too long you can always show an intermediate thing...) Commented Apr 6 at 9:33
  • If the file is already generated, what do you need the about:blank page for? Do you mean you generate a file with the same name (i.e., you overwrite the same file over and over)? If so, then why not just refresh the page, so it's not - hypothetically - loaded from cache? -- Is this a real scenario, or are you just asking about the functionality (maybe in a completely different context) of TaskCompletionSource? Commented Apr 6 at 20:13

0

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.