ASP.NET Core – How to configure Angular2 with TypeScript

This article will show how to configure Angular 2 and TypeScript in a ASP.Net Core based API

Download code here:

STEP 1 – Make sure you have installed the prerequisites

Without these requisites, application will not run.

STEP 2 – Create ASP.NET Core Web Application

Go to Visual StudioFile New Project menu, expand the Web category, and pick ASP.NET Core Web Application like on the image below

Select the template Web API:


STEP 3 – Prepare Web Application to run Angular 2

At this point, we have our backend part ready. Let’s setup the frontend.

A Web API project can’t serve static files like JavaScripts, CSS styles, images, or even HTML files. Therefore we need to add a reference to Microsoft.AspNetCore.StaticFiles in the project.json:

  • “Microsoft.AspNetCore.StaticFiles”: “1.0.0 “,

And in the startup.cs, we need to add the following line, just before the call of UseMvc().

  • app.UseStaticFiles();

Another important thing we need to do in the startup.cs is to support the Routing of Angular 2. If the Browser calls a URL that doesn’t exist on the server, it could be an Angular route. Especially if the URL doesn’t contain a file extension.

Just before we call UseStaticFiles() insert the following code:

app.Use(async (context, next) => 
    await next(); 
    if (context.Response.StatusCode == 404 
        && !Path.HasExtension(context.Request.Path.Value)) 
        context.Request.Path = "/index.html"; 
        await next(); 

STEP 4 – Configure Angular 2


Now we prepared the ASP.NET Core application to start to follow the tutorial.

  • You need to create tsconfig.json which is the TypeScript compiler configuration file. It guides the compiler to generate JavaScript files.
  "compilerOptions": { 
  "exclude": [ 
  •  Create a typings.json file in your project folder angular2-demo as shown below:
  "globalDependencies": { 
  • Add package.json file to your project folder with the below code:
  "scripts": { 
    "start""concurrent \"npm run tsc:w\" \"npm run lite\" ", 
    "tsc:w""tsc -w", 
    "postinstall""typings install" 
  "dependencies": { 
  "devDependencies": { 
The package.json will contain the packages that our apps require. These packages are installed and maintained with npm (Node Package Manager). To install packages, run the below npm command in command prompt.

Create a sub-folder called app/ inside your wwwroot project folder to the place Angular app components.

The files which you create need to be saved with .ts extension. Create a file called environment_app.component.ts in your app/ folder with the below code:

import {Component, View} from "angular2/core"; 
   selector: 'my-app' 
  template: '<h2>My First Angular 2 App</h2>' 
export class AppComponent { 
Next, create environment_main.ts file with the below code:
import {bootstrap} from "angular2/platform/browser" 
import {AppComponent} from "./environment_app.component" 
The environment_main.ts file tells Angular to load the component.
  • Now create a index.html in your project folder angular2demo/ with the below code:
<!DOCTYPE html> 
    <title>Hello World</title> 
        transpiler: 'typescript', 
        typescriptOptions: { emitDecoratorMetadata: true }, 
        packages: {'app': {defaultExtension: 'ts'}} 
            .then(null, console.error.bind(console)); 
Angular will launch the app in the browser with our component and places it in a specific location on index.html.

STEP 5 – Run application




About João Sousa
Senior Software Engineer in .Net (Microsoft Certified MTCS and MCPD). .NET Microsoft MVP 2015

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: