Angular 5 URL重写的问题

问题描述 投票:1回答:1

我用两条路线构建了一个Angular 5应用程序。我目前有:Home / DailySummary和Home / Documents。当我将应用程序发布到服务器(IIS 6.0 w / URL Rewrite)并导航到Home.aspx页面时,一切都运行良好,我的路由将我指向DailySummary和Documents,如果我点击这些链接。我遇到的问题是刷新或导航到页面而不去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 url-rewriting url-routing iis-6
1个回答
2
投票

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

我发现解决这个问题的最简单方法是完全避免这个问题并使用文档称之为“旧学校”的方法 - 将路由器切换到HashLocationStrategy。你最后使用http://mydomain/index.html#somelocation而不是http://mydomain/somelocation这样的网址,但用户真的关心吗?

结果是“丑陋”的URL只是工作 - 书签和重新加载工作得很好,因为你总是加载index.html

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

© www.soinside.com 2019 - 2024. All rights reserved.