asp.net-mvc - Angular 2 路由问题 - 刷新或重新加载时出现 404 错误

标签 asp.net-mvc angular

我正面临与 Angular 路由相关的问题。所有页面都工作正常,但如果重新加载或刷新页面,则会出现 404 错误。我正在使用路径定位策略。我搜索了很多但发现了哈希位置策略之外的东西。我尝试了哈希位置策略但是在 url 中添加了#(如 webiste.com/home#contact)但我想要没有#的 url。

我需要专家指导,因为我在搜索 3 4 天后卡住了

提前致谢

最佳答案

根据您使用的服务器,您需要将路由配置为始终指向 index.html。

当然需要设置<base href="/"><head>你的index.html

如果刷新 url site.com/myPage ,服务器将尝试获取名为 myPage 的资源,在您的情况下将返回 404。

WAMP 设置

Options -Indexes

RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) index.html [NC,L]

IIS 设置

  <system.webServer>
    <rewrite>
      <rules>
        <rule name="AngularJS Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

关于asp.net-mvc - Angular 2 路由问题 - 刷新或重新加载时出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392577/

相关文章:

jquery - MVC 同时返回 json 和 html 响应

asp.net-mvc - Azure 网站,中型实例,2 核,多少个应用程序池?

angular - firebase_app__WEBPACK_IMPORTED_MODULE_5__.app(...).functions 不是函数

angular - 从 Nebular NbTreeGridComponent 更改列的宽度

c# - 如何发布到 Facebook 用户的个人资料?

javascript - 将我的 .net MVC View 中的 bool 值传递给 javascript,但 JS 不接受 True,想要 true

c# - 如何在 ASP.NET MVC 中使用选择列表返回多个值?

javascript - ng2-smart-table 删除后不绑定(bind)

Angular,如何在 FormArray 的特定 FormControl 中设置焦点

angular - 从 RXJS 5 迁移到 6 - IntervalObservable