You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MVC中WebView2无法加载HTML字符串(Uri加载正常)

WebView2加载本地HTML字符串失败并触发无限导航循环问题

我们正在将大量旧的IE11浏览器和CefSharp组件升级为WebView2,在MVC应用中,通过Source指向Uri加载网页可正常工作,但加载本地HTML字符串时出现异常。

相关代码

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

待加载的HTML字符串(vHTML)

<!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>

问题现象

  • 程序会触发NavigationStarting和NavigationCompleted事件,但页面无内容加载,右键打开DevTools仅能看到基础的html、head和body标签。
  • 【更新】调试发现,调用NavigateToString时,NavigationStarting和NavigationCompleted会持续触发无限循环;而使用Source加载Uri时仅触发两次。添加计数器后发现,加载字符串的页面会持续循环直至弹窗关闭,几秒内计数就超过24000次。

内容的提问来源于stack exchange,提问作者gchq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 14:17:17