Router in Angular 8 return 404 when deploy to IIS Server
Hello, I have a small project with angular 8. Everything well on the local computer but when I deploy my project to my server IIS but I got an error 404 file or directory not found.
localhost
localhost:4200/product - I can see the component
On IIS server
exapme.mysite.com/product - Only see the error 404
My router config
import { Routes, RouterModule } from '@angular/router';
import { ProductComponent } from './product/index';
const appRoutes: Routes = [
{ path: '/product', component: ProductComponent }
// otherwise redirect to home
{ path: '**', redirectTo: '' }
];
export const routing = RouterModule.forRoot(appRoutes);
How can I resolve this issue?
-
S2
Sandeep Kumar May 21 2021
A way to fix this issue is by using
HashLocationStrategy
. You can include these lines in the provider array of yourapp.module.ts
file:import { LocationStrategy, HashLocationStrategy, PathLocationStrategy } from '@angular/common'; { provide: LocationStrategy, useClass: HashLocationStrategy }
'LocationStrategy' is responsible for representing and reading route state from the browser's URL. Angular provides two strategies:
{@link HashLocationStrategy}
and{@link PathLocationStrategy}
.It worked for me.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.