Angular 5 URL 重写的问题

Issues with Angular 5 URL Rewrite

我构建了一个带有两条路由的 Angular 5 应用程序。我目前有: Home/DailySummary 和 Home/Documents。当我将应用程序发布到服务器(带 URL 重写的 IIS 6.0)并导航到 Home.aspx 页面时,一切都运行良好,如果我单击这些链接,我的路由会将我定向到 DailySummary 和文档。我遇到的问题是刷新或导航到页面而不去 Home.aspx,它将我定向到根文件夹。我确定这与我的 URL 重写规则有关:

<rule name="RewriteRules" 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="Redirect" url="/NewWebApp/" />
</rule>

我的 Home.aspx 页面具有相同的 href 值 ("/NewWebApp/")

我不确定如何前往 Home.aspx 然后导航到特定路线。

我的Web.config和Home.aspx在我项目的根目录下

如果有帮助,这是我的路线。

const appRoutes: Routes = [
{ path: 'Home.aspx', redirectTo: 'Home/DailySummary', pathMatch: 'full' },
{ path: '', redirectTo:'Home/DailySummary', pathMatch:'full' },
{
    path: 'Home', component: INHome, children: [
        { path: 'DailySummary', component: DailySummary },
        { path: 'Documents', component: Documents },
        { path: 'Safety',component: Safety},
        { path:'', redirectTo:'DailySummary', pathMatch:'full'}
    ]
},
{path:'**',redirectTo:'Home/DailySummary', pathMatch:'full'}
]

感谢您的帮助。如果您需要更多信息,请告诉我。

这是 Angular 的 PathLocationStrategy 工作方式的问题。 它生成漂亮的 URL,但它展示了您正在经历的行为 - 除非您进行大量黑客攻击,否则用户必须将应用程序的索引页面加载到 bootstrap 应用程序,因此将书签加载到其他位置(和重新加载)不起作用。

我发现解决此问题的最简单方法是完全避免该问题并使用文档中称为 "old school" 的方法 - 将您的路由器切换到 HashLocationStrategy。您最终会得到像 http://mydomain/index.html#somelocation instead of http://mydomain/somelocation 这样的 URL,但用户真的在乎吗?

结果是 "ugly" 正常工作的 URL - 书签和重新加载工作正常,因为您总是在加载 index.html

有关详细信息,请参阅 https://angular.io/guide/router#browser-url-styles