#author("2023-10-07T11:13:12+08:00","default:Admin","Admin")
#author("2023-10-07T11:14:11+08:00","default:Admin","Admin")
[[Vue]]

&color(red){※前提条件:本文基于 Vue 2.0 创作};
#contents

* 浏览器刷新404错误 [#cdce6056]

* 原因 [#p52b8cc9]

问题原因我就不说了,官网已经说了,想了解的看这里
 https://router.vuejs.org/zh/guide/essentials/history-mode.html#%E5%90%8E%E7%AB%AF%E9%85%8D%E7%BD%AE%E4%BE%8B%E5%AD%90


* 对策 [#x1027018]

在网上搜了大部分的IIS的解决方案如下:

** 安装 IIS UrlRewrite [#vc57e29c]

** 修改 web.config [#tceb297f]

在你的网站根目录中创建一个 web.config(如果已经存在,就不用创建了) 文件,内容如下:

#codeprettify{{
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
}}


但是完全按照这个配置后,刷新页面没有问题了,但是请求却不重新获取数据了。

*** 原因 [#i1a6a206]

添加配置后Cache-Control被改成了private,代表只有页面第一次加载的时候才向服务器请求数据。

*** 解决 [#a4557438]
*** 解决方法1 [#a4557438]

将匹配的正则表达式<match url="(.*)" /> 改成 <match url="(home)" /> 因为网站每一个页面链接都包含“home”(http://aaa.bbb.com/home/desktop/process),但是请求数据的链接是:http://aaa.bbb.com/api/Account/Login。修改后,配置只对页面链接起做用。
这样刷新后既不报404错误,也能获取到后端数据了。
将匹配的正则表达式<match url="(.*)" /> 改成 <match url="(home)" /> 因为网站每一个页面链接都包含“home”
 http://aaa.bbb.com/home/desktop/process

但是请求数据的链接是:
 http://aaa.bbb.com/api/Account/Login

修改后,配置只对页面链接起做用。这样刷新后既不报404错误,也能获取到后端数据了。

*** 解决方法2 [#ie63f650]

或者如下:匹配所有不包含api的数据请求

#codeprettify{{
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
          <match url="(^(?!.*?api).*$)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
}}


#hr();
コメント:
#comment_kcaptcha

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS