刷新我的 Angular 页面给我一个 404 服务器错误,如果我手动导航到搜索栏中的目录,它也会发生同样的情况。我通过 IIS 服务器托管。
只有在我使用 ng build --prod
构建 Angular 项目时才会出现此问题然后将其上传到我的网络服务器。很多人说你应该使用散列方法,但我不想在我的 url 中显示散列,这是一种非常古老的方法。有些人还说我应该改变我的 <base>
在我的标签 <head>
,但这并没有奏效。
这是我的 <head>
来 self 的 index.html 的标记(如果有帮助的话)。
<head>
<!-- META DATA -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- APP -->
<title>SebastianElstadt</title>
<base href="/">
<link rel="icon" type="image/x-icon" href="assets/images/app-icon.png">
<!-- STYLES -->
<link rel="stylesheet" href="[fontawesome cdn]">
</head>
我还使用了一个组件,如果输入的 URL 与我定义的任何路径都不匹配,该组件就会显示出来。但它没有显示。
总而言之,每当我刷新我的页面或在我的 Angular 站点的搜索栏中手动导航时,它都会显示 404 服务器错误页面。而且我不想使用散列方法。
最佳答案
您可以通过创建 web.config 文件来解决此问题:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.html" />
</files>
</defaultDocument>
<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" />
</conditions>
<action type="Rewrite" url="/" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
<security>
<requestFiltering allowDoubleEscaping="true">
<fileExtensions>
<add fileExtension=".json" allowed="true" />
</fileExtensions>
</requestFiltering>
</security>
<directoryBrowse enabled="true" />
<staticContent>
<remove fileExtension=".woff" />
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
<mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
</staticContent>
</system.webServer>
</configuration>
.Net Core with Angular 4 : 404 error when I refresh the browser
关于javascript - 刷新后 Angular 6 项目显示 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54480750/