Issues with Angular 5 URL Rewrite

故事扮演 提交于 2020-06-16 03:52:08

问题


I built an Angular 5 application with two routes. I currently have: Home/DailySummary and Home/Documents. When I publish the application to the server (IIS 6.0 w/ URL Rewrite) and navigate to the Home.aspx page everything runs perfectly and my Routing directs me to DailySummary and Documents if I click those links. The issue I'm having is refreshing or navigating to the page without going to Home.aspx, it directs me to the root folder. I'm certain this has something to do with my URL rewrite rule:

<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>

My Home.aspx page has the same href value ("/NewWebApp/")

I'm not sure how to go to Home.aspx then navigate to the specific route.

My Web.config and Home.aspx are in the root directory of my project.

Here's my routes if it helps.

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'}
]

Thanks for your help. Let me know if you need more info.


回答1:


This is an issue with the way Angular's PathLocationStrategy works. It produces nice, pretty URLs, but it exhibits the behavior that you are experiencing - unless you do a bunch of hacking around, the user has to load the app's index page to bootstrap the application, and so bookmarks to other locations (and reloading) doesn't work.

I find that the easiest way to fix this is to avoid the problem altogether and use what the documentation calls the "old school" method - switch your router to the HashLocationStrategy. You wind up with URLs like http://mydomain/index.html#somelocation instead of http://mydomain/somelocation, but do users really care?

The result is "ugly" URLs that just work - bookmarks and reloads work just fine, because you're always loading index.html

See https://angular.io/guide/router#browser-url-styles for more info.



来源:https://stackoverflow.com/questions/49074928/issues-with-angular-5-url-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!