首页 > 解决方案 > Html base href 重写在 200 之后获取第一个 404

问题描述

我在 IIS 站点的子目录下有一个 blazor 应用程序。要修复 blazor wasm 基本路径问题,请在 head 标记中应用下面的 javascript 代码(在 elmah.io 上建议:https ://blog.elmah.io/how-to-fix-blazor-wasm-base-path-problems/ )

<!-- omitted for clarity -->
<head>
  <base />
  <script>
      var path = window.location.pathname.split('/');
      var base = document.getElementsByTagName('base')[0];
      if (window.location.host.includes('localhost')) {
          base.setAttribute('href', '/MyApp/');
      } else if (path.length > 2) {
          let href = '/' + path[1] + '/MyApp/';
          base.setAttribute('href', href);
      } else if (path[path.length - 1].length != 0) {
          let fullpath = window.location.origin + window.location.pathname + '/' + window.location.search;
          window.location.replace(fullpath);
      }
  </script>
</head>

它工作得很好,解决了 blazor 基本路径问题。但是,当您输入不以“/”结尾的应用程序 url 时,开发人员控制台会填满 404(未找到)错误,然后对于所有 css/js 文件,所有错误都会显示为 200(ok)ok。为什么会这样,我可以修复它吗?

安慰 在网络视图中,所有资源在 200 之后获得第一个 404

标签: javascripthtmlblazorblazor-webassembly

解决方案


推荐阅读