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:
- Navigate to
about:blank - 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
path-to-generated-file.htmlmay be storing old data, and the navigation toabout:blankis 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 issueTaskCompletionSource?