简体   繁体   中英

Angular router removes query params when deployed on IIS server

I try to use Angular router but I have some troubles when the app is deployed. First, here is my versions:

Angular CLI: 6.2.3
Node: 9.11.1
OS: win32 x64
Angular: 6.1.9
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.8.3
@angular-devkit/build-angular     0.8.3
@angular-devkit/build-optimizer   0.8.3
@angular-devkit/build-webpack     0.8.3
@angular-devkit/core              0.8.3
@angular-devkit/schematics        0.8.3
@angular/cli                      6.2.3
@ngtools/webpack                  6.2.3
@schematics/angular               0.8.3
@schematics/update                0.8.3
rxjs                              6.2.2
typescript                        2.9.2
webpack                           4.20.2

I want to have 2 possible routes:

  • mydomain.com/TestRouting/my-route
  • mydomain.com/TestRouting/my-route?route=whatever

It seems quite easy, right ?

Here is my routes definition in a specific module:

import ...

const appRoutes = [
  { path: 'my-route', component: RouteComponent },
  { path: '', redirectTo: '/my-route', pathMatch: 'full' }
];

@NgModule({
  imports: [
    CommonModule,
    RouteModule, // This is a module containing the RouteComponent
    RouterModule.forRoot(appRoutes)
  ],
  declarations: [],
  exports: [
    RouterModule
  ]
})
export class RoutingModule { }

Of course, I've imported this module in the AppModule, added the router-outlet markup in the app.component.html

In my RouteComponent, I get the query param this way:

export class RouteComponent implements OnInit {
  urlParam: string;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    console.log(this.route.snapshot);
    this.urlParam = this.route.snapshot.queryParams['route'];
  }

}

Since, I've deployed the website on a IIS Server, I've added this web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="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="^/(api)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/TestRouting" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

THE ISSUE: In local, it works well... (ng serve) In local and production mode, it works well too (ng serve --prod)

I build the app with this command: ng build --base-href /TestRouting/ --prod

Once deployed,

mydomain.com/TestRouting/ redirects to mydomain.com/TestRouting/my-route (expected behavior) mydomain.com/TestRouting/my-route renders the component (expected behavior) BUT mydomain.com/TestRouting/my-route?route=test redirects to mydomain.com/TestRouting/my-route (well, that's not ok...)

Anyone has an idea ? Thanks a lot for your help.

This has nothing to do with Angular. You are rewriting the url with /TestRouting and ignoring the query string, try this:

<action type="Rewrite" url="/TestRouting" appendQueryString="true" />

Thanks to @ritaj, I solve the problem using the HashLocation strategy . It changed the url template but it works well!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM