Bharat
Bharat

Reputation: 2462

Angular 2 error when publish on iis

I have created demo application in angular 2, its working fine when i run it on local but getting error while i published it on iis serve.

This error i am getting

Failed to load resource: the server responded with a status of 404 (Not Found)

enter image description here

Here is my code index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>MyApp</title>
  <base href="/">
  <link rel="stylesheet" href="https://bootswatch.com/darkly/bootstrap.min.css">  
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script>
    System.import('app').catch(function(err){ console.error(err); });
  </script>
</head>
<body>
  <app-root>Loading...</app-root>
</body>

</html>

app.router.ts

import {ModuleWithProviders} from '@angular/core';
import {Routes,RouterModule} from '@angular/router'; 

//import {AppComponent} from './app.component'
import {AboutComponent} from './about/about.component'
import {TechnologyComponent} from './technology/technology.component'

export const router : Routes = [
    {path:'',redirectTo:'about',pathMatch:'full'},
    {path:'about',component:AboutComponent},
    {path:'tachnology',component:TechnologyComponent}
]
export const routes: ModuleWithProviders = RouterModule.forRoot(router)

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

//import { HashLocationStrategy, LocationStrategy } from '@angular/common'

import {routes} from './app.router'
import { AppComponent } from './app.component';
import { AboutComponent } from './about/about.component';
import { TechnologyComponent } from './technology/technology.component';

import {AboutSrvice} from './about/about.service'
@NgModule({
  declarations: [
    AppComponent,
    AboutComponent,
    TechnologyComponent,
  ],

  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routes
  ],
  //There is two ways you can use service first here you can add service in provider or either you can add it in about.component.ts file in @Component({}) section 
 // providers: [AboutSrvice,{provide: LocationStrategy, useClass: HashLocationStrategy}],
 providers: [AboutSrvice],
  bootstrap: [AppComponent]
})
export class AppModule { }

Is there am i missing something or need any extra content for publish?.

Upvotes: 6

Views: 2386

Answers (3)

Suraj Sakhare
Suraj Sakhare

Reputation: 363

Same problem was happen with me, I just made changes in index.html

<base href="/websitename/"> 

Upvotes: 1

Tarek.Eladly
Tarek.Eladly

Reputation: 759

it terns out that it is a conflict in versions so

let us play :D

npm uninstall -g @angular/cli
npm cache clean

then after that

npm install -g @angular/cli@latest --save

create new project in a new folder

ng new TheProjectName

close the editor

copy src from the old project to the new project

run the editor

build and deploy

ng build --prod --aot --no-sourcemap --base-href

Upvotes: 1

Swoox
Swoox

Reputation: 3740

Ok the problem is Angular make everything with a base ref "/". So the index.html it create will include files with /filename.

What you have to do is remove the base ref and change it to <base href="">

Then look in your webpack files and remove in webpack.prod. change:

publicPath: '/',

Into:

publicPath: '',

Now if use routes read this https://angular.io/api/common/HashLocationStrategy and implement it.

EDIT

For CLI instead of changing the .prod webpack you can do:

ng build --prod --aot --no-sourcemap --base-href ""

Upvotes: 1

Related Questions