在大多数情况下,Blazor将与以前相同的线路上重新连接到服务器。但有时无法重新连接,需要重新加载web浏览器才能使网站重新工作。如果服务器回收应用程序池,则需要手动重新加载页面
在没有调试的情况下在IIS Express上开发和运行站点时,使用自动重新加载可以加快开发过程。只需保存您的文件并切换到web浏览器,它将在编译完所有内容并准备就绪时自动刷新。
有一种方法可以自动重新加载浏览器。 前一段时间,丹·罗斯(Dan Roth)在Github上发布了一个解决方案,将以下脚本粘贴到_host.cshtml中。 这使用JS DOM mutation observer API来检测“重新加载”按钮何时可见,并自动重新加载页面。

<script>
// 等待直到出现“重新加载”按钮
new MutationObserver((mutations, observer) => {
if (document.querySelector('#components-reconnect-modal h5 a')) {
// 现在,每隔10秒,查看服务器是否返回,如果返回,则重新加载
async function attemptReload() {
await fetch(''); // 检查服务器是否真的返回
location.reload();
}
observer.disconnect();
attemptReload();
setInterval(attemptReload, 10000);
}
}).observe(document.body, { childList: true, subtree: true });
</script>

  

更多查看 https://github.com/dotnet/aspnetcore/issues/10325

05-11 22:22