The source code of this project is added into GitHub, you can download, clone or submit your changes to this repository.
Link to GitHub repository
https://github.com/shibut12/ng5-netcore

The project is added to Travis CI to auto build.
Tavis ci build status

My system configuration

  • @angular/cli: version 1.5.5
  • Node: Version 6.11.0
  • npm: Version 3.10.10
  • dotnet: Version 2.0.2

First things first

Before you begin creating a project and updating, let’s look into preparing the environment before we create the project. Run following commands in Command Prompt or shell.

mkdir ng-netcore
cd ng5-netcore
npm uninstall -g @angular/cli
npm cache clean
npm install -g @angular/cli@latest
npm install -g npm-check-updates

Create angular app

Now, lets create a aspnet core 2 web applicaiton using dotnet angular SPA temaplte. Runt following commands in command prompt.

dotnet new angular
npm install
ncu
ncu -u
npm cache clean
npm install
C:\Users\sthannikkunnath\source\repos\ng5-netcore2>ncu
Using C:\Users\sthannikkunnath\source\repos\ng5-netcore2\package.json
[..................] \ :
 @angular/animations                 4.2.5  →         5.0.5
 @angular/common                     4.2.5  →         5.0.5
 @angular/compiler                   4.2.5  →         5.0.5
 @angular/compiler-cli               4.2.5  →         5.0.5
 @angular/core                       4.2.5  →         5.0.5
 @angular/forms                      4.2.5  →         5.0.5
 @angular/http                       4.2.5  →         5.0.5
 @angular/platform-browser           4.2.5  →         5.0.5
 @angular/platform-browser-dynamic   4.2.5  →         5.0.5
 @angular/platform-server            4.2.5  →         5.0.5
 @angular/router                     4.2.5  →         5.0.5
 @ngtools/webpack                    1.5.0  →         1.8.5
 @types/webpack-env                 1.13.0  →        1.13.2
 awesome-typescript-loader           3.2.1  →         3.4.1
 css-loader                         0.28.4  →        0.28.7
 event-source-polyfill               0.0.9  →        0.0.12
 expose-loader                       0.7.3  →         0.7.4
 extract-text-webpack-plugin         2.1.2  →         3.0.2
 file-loader                        0.11.2  →         1.1.5
 html-loader                         0.4.5  →         0.5.1
 json-loader                         0.5.4  →         0.5.7
 preboot                             4.5.2  →  6.0.0-beta.0
 rxjs                                5.4.2  →         5.5.4
 style-loader                       0.18.2  →        0.19.0
 typescript                          2.4.1  →         2.6.2
 url-loader                          0.5.9  →         0.6.2
 webpack                             2.5.1  →        3.10.0
 webpack-hot-middleware             2.18.2  →        2.21.0
 webpack-merge                       4.1.0  →         4.1.1
 zone.js                            0.8.12  →        0.8.18
 @types/chai                         4.0.1  →         4.0.8
 @types/jasmine                     2.5.53  →         2.8.2
 chai                                4.0.2  →         4.1.2
 jasmine-core                        2.6.4  →         2.8.0
 karma                               1.7.0  →         1.7.1
 karma-jasmine                       1.1.0  →         1.1.1
 karma-webpack                       2.0.3  →         2.0.6

Run ncu with -u to upgrade package.json and run ncu again to check if there are any out dated packages.

C:\Users\sthannikkunnath\source\repos\ng5-netcore2>ncu -u
C:\Users\sthannikkunnath\source\repos\ng5-netcore2>ncu
Using C:\Users\sthannikkunnath\source\repos\ng5-netcore2\package.json
[..................] - :
All dependencies match the latest package versions :)

Update source code to match changes in Angular 5

1. Modify app.module.shared.ts

  1. Replaceimport { HttpModule } from '@angular/http';  withimport { HttpClientModule } from '@angular/common/http';
  2. Import HttpModule instead of HttpClientModule

Complete app.module.shared.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './components/app/app.component';
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';

@NgModule({
    declarations: [
        AppComponent,
        NavMenuComponent,
        CounterComponent,
        FetchDataComponent,
        HomeComponent
    ],
    imports: [
        CommonModule,
        HttpClientModule,
        FormsModule,
        RouterModule.forRoot([
            { path: '', redirectTo: 'home', pathMatch: 'full' },
            { path: 'home', component: HomeComponent },
            { path: 'counter', component: CounterComponent },
            { path: 'fetch-data', component: FetchDataComponent },
            { path: '**', redirectTo: 'home' }
        ])
    ]
})
export class AppModuleShared {
}

 

2. Modify webpack.config.js

  1. Replaceconst AotPlugin = require('@ngtools/webpack').AotPlugin;  with const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
  2. Replace new AotPlugin with new AngularCompilerPlugin

Complete webpack.config.js

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: [ '.js', '.ts' ] },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /\.ts$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: [ 'to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
                { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [new CheckerPlugin()]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'main-client': './ClientApp/boot.browser.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
            // Plugins that apply in production builds only
            new webpack.optimize.UglifyJsPlugin(),
            new AngularCompilerPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.module.browser#AppModule'),
                exclude: ['./**/*.server.ts']
            })
        ])
    });

    // Configuration for server-side (prerendering) bundle suitable for running in Node
    const serverBundleConfig = merge(sharedConfig, {
        resolve: { mainFields: ['main'] },
        entry: { 'main-server': './ClientApp/boot.server.ts' },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./ClientApp/dist/vendor-manifest.json'),
                sourceType: 'commonjs2',
                name: './vendor'
            })
        ].concat(isDevBuild ? [] : [
            // Plugins that apply in production builds only
            new AngularCompilerPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.module.server#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

3.Modify fetchdata.component.ts

  1. Update import { Http } from '@angular/http'; to import { HttpClient } from '@angular/common/http';
  2. In the constructor signature, replace Http with HttpClient
  3. In the constructor, update result.json as WeatherForecast[]; to result as WeatherForecast[];

Complete fetchdata.component.ts

import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
    selector: 'fetchdata',
    templateUrl: './fetchdata.component.html'
})
export class FetchDataComponent {
    public forecasts: WeatherForecast[];

    constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
        http.get(baseUrl + 'api/SampleData/WeatherForecasts').subscribe(result => {
            this.forecasts = result as WeatherForecast[];
        }, error => console.error(error));
    }
}

interface WeatherForecast {
    dateFormatted: string;
    temperatureC: number;
    temperatureF: number;
    summary: string;
}

Build and run aspnet core 2 Angular 5 application

dotnet restore
dotnet build
dotnet publish
dotnet run

Verify Application is loading

Verify HttpClient is working