Router in Angular 8 return 404 when deploy to IIS Server

Dung Do Tien May 21 2021 342

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.

Router in Angular 8 return 404 when deploy to IIS Server

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?

Have 1 answer(s) found.
  • S

    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 your app.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.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close