首页 文章

角度部署 - 页面刷新404

提问于
浏览
0

我想在Angular中将我的应用程序部署到 生产环境 服务器,但我遇到了问题 . 当我只使用角度路由(更改组件,而不是重定向)时,应用程序可以正常运行,但是当我在浏览器中刷新页面时,我从IIS返回一个404页面(我使用IIS作为Web服务器)

这是我的角度路由:

const appRoutes: Routes = [
    { path: '', redirectTo: '/home', pathMatch: 'full', canActivate: [AuthGuard] },
    { path: 'home', component: DashboardComponent, canActivate: [AuthGuard] },
    { path: "profile", component: UserProfileComponent, canActivate: [AuthGuard] },
    { path: '400', component: ErrorComponent },
    { path: '401', component: ErrorComponent },
    { path: '403', component: ErrorComponent },
    { path: '404', component: ErrorComponent },
    { path: '500', component: ErrorComponent },
    { path: '503', component: ErrorComponent },
    { path: '**', redirectTo: '/404' }
]

1 回答

  • 4

    我在我的应用程序中修改了web.config:

    <configuration>
      <system.webServer>
    <rewrite>
        <rules>
            <rule name="redirect all" stopProcessing="true">
                <match url="^(.*)$" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
                </conditions>
                <action type="Rewrite" url="./" appendQueryString="true" />
            </rule>
        </rules>
    </rewrite>
      </system.webServer>
    </configuration>
    

    在index.html中是 <base href="./"> . 刷新页面现在可以了 .

相关问题