【问题标题】:Angular4+ router redirects to home page in IISAngular 路由器重定向到 IIS 中的主页
【发布时间】:2018-07-18 19:34:56
【问题描述】:

几周以来我一直在学习 Angular 4+。

我创建了一个在开发模式下运行良好的 Web 应用程序,但是当我第一次尝试在 IIS 中部署该应用程序时,由于众所周知的原因而卡住了

404 页面未找到

直接从浏览器地址栏输入地址时出错。

我将各种线程中描述的解决方案应用为this one.

我的应用程序的基本 href 是“/ePortal/”,我的主要应用程序路由定义如下:

export const appRoutes: Routes = [
   { path: 'login', component: LoginComponent },
   { path: 'logout', component: LogoutComponent },
   { path: '', redirectTo: '/publicarea', pathMatch: 'full' },
   { path: '**', component: PageNotFoundComponent }
];

然后我有一个子路由定义为:

const publicAreaRoutes: Routes = [{
   path: 'publicarea',
   component: PublicAreaComponent,
   children:[
      { path: 'events', component: EventsComponent},
      { path: 'comunicazioni', component: ComunicazioniComponent},
   ]
}];

问题是,如果我尝试使用地址栏导航到:

http://localhost/ePortal/publicarea/events

我不断被重定向到

http://localhost/ePortal/publicarea

正如我之前所说,该应用程序在 dev 中运行良好,问题可能是由于我用来修复 404 Page not found 错误的 IIS 规则重写和路径重定向到 publicare 时路径的组合''

{ path: '', redirectTo: '/publicarea', pathMatch: 'full' },

这是我正在使用的 web.config 文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <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="^/$" negate="true"/>
              </conditions>
              <action type="Rewrite" url="/ePortal" />
           </rule>
        </rules>
     </rewrite>
  </system.webServer>
</configuration>

关于如何解决这个问题的任何建议?我错过了什么吗? 非常感谢

【问题讨论】:

  • 尝试在重写规则后面添加一个斜线? &lt;action type="Rewrite" url="/ePortal/" /&gt;
  • 你在使用 angular-cli 吗?

标签: angular iis url-rewriting router


【解决方案1】:

也许你需要做的就是改变

<action type="Rewrite" url="/ePortal" />

<action type="Rewrite" url="/ePortal/index.html" />

当你重定向到根目录时,它会删除 url 的结尾,你需要直接引入 indexfile。

以下是有关此问题的有用信息: https://gingter.org/2017/03/20/deep-link-angular-spa-iis/

【讨论】:

    【解决方案2】:

    在部署到 IIS 时,您应该使用 HashLocationStrategy。

    https://angular.io/api/common/HashLocationStrategy

    如果需要,请查看this 问题。

    【讨论】:

    • 您是否有任何说明您应该在 IIS 中使用 HashLocationStrategy 的文档的链接?
    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2016-10-10
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多