我有一个问题,重定向我的应用程序在生产模式。当我有url http://server.com/projectname/dashboard时,服务器响应IIS错误页404。我必须使用http://server.com/projectname打开应用程序,然后单击链接,例如:
<a [routerLink]="'dashboard'">Dashboard</a>在html中,我有<base href="./">,这是我的路由器:
const appRoutes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full', canActivate: [AuthGuard] },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard]},
{ path: '400', component: ErrorComponent, canActivate: [AuthGuard] },
{ path: '401', component: ErrorComponent, canActivate: [AuthGuard] },
{ path: '404', component: ErrorComponent, canActivate: [AuthGuard],
{ path: '**', redirectTo: '/404' }
]
@NgModule({
imports: [
RouterModule.forRoot(appRoutes)
]
})在开发模式中,我有url http://localhost:4200/dashboard,我应该重定向。当开发是url localhost:4200/仪表板,生产中是server.com/appname/dashboard?时,会出现问题吗?
发布于 2017-11-29 08:49:07
我找到了原因。我必须配置退路。这里是一篇关于它的文章。
我不得不添加web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Angular 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="./" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>现在它工作正常,我可以打开任意的应用程序url。
发布于 2017-11-29 08:48:36
您需要更新应用程序的基本href。如果您正在使用angular-cli,我不会推荐@Daniel的答案,因为有一种更简单的方法。
使用angular-cli,您可以使用:ng build --prod --bh /appname/,这将更新生产生成基href。这是一种更好的方法,因为在切换环境时,不必在index.html中手动更新。
发布于 2017-11-29 08:37:50
在生产上部署时,在index.html上添加这一行:
<base href="/appname/">https://stackoverflow.com/questions/47548281
复制相似问题