我正面临与 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/