Angular 2 Quickstart with ASP.NET Core

Updated version of this post can be found here.

After working with Aurelia over last couple of months it seemed prudent to try out Angular 2. This post is going to cover the installation of Angular 2 and launching a basic Angular 2 application from an ASP.NET Core (the new name for ASP.NET 5) controller. Make sure to check out the official quickstart guide here.

Installation

Open the package.json file and add the following dependencies and devDependencies. This project is using gulp and is using the typescript based version of the quickstart guide which is why the gulp and typescript references are there.

If inside Visual Studio 2015 the proper packages will be restored when package.js is saved, but outside of Visual Studio open a command prompt to the same directory as the package.js file and run  npm install  which will download the required packages.

Typescript Configuration

Next add a tsconfig.json file to the project. This file marks the root of a typescript project and contains configuration setting for that project. Details on configuration options can be found here.

Angular 2 Bootstrap and Basic App

Create an Angular folder in the project’s root folder. Next add a app.component.ts file which is used to manage a view. This component is super simple and was pulled from the official quickstart guide.

Next add boot.ts which is used to bootstrap Angular 2.

Gulp Tasks

This is the project that was the inspiration for the gulpjs introduction post. The first task is to move the Angular 2 dependencies from the node_modules folder to an Angular folder in wwwroot  so they will be available to be served.

The next task move the js files created by the typescript compiler when a ts file is saved from the Angular folder in the root of the project to wwwroot/Angular.

The last task just runs both of the previous tasks.

ASP.NET Controller

The Angular application is going to be run from the ASP.NET’s HomeController. Here is the new action that will load the yet to be created view.

ASP.NET View

This is the view that when loaded kicks off the Angular 2 application. Add a new file called Angular2.cshtml in the Views/Home folder.  In the following first Angular related scripts are loaded. Then system.js is configured and the boot module is imported and the app is rendered inside of the my-app tag.

This is where the trouble started for me. It took a bit of searching, but the problem turned out to be with the system.js configuration. Since the view is in the HomeController it gets rendered out of as Home/Angular2.html and this project’s Angular files are up a directory so in order for system.js to find the Angular files the package configuration needed to be set to look up one folder.

This issue took longer to find than I would like to admit, but that is the danger is using libraries one is not familiar with. System.js was not my first thought since it was used in my Aurelia without issue. If you run into an issue check out the docs for system.js configuration.

Finally add the new view to main application’s nav bar. In Views/Shared/_Layout.cshtml add a link to Angular 2 on using the Home controller.

Follow Up

This is a very basic Angular 2 application. Expect a follow up post in the new few weeks with this application build out more to utilize the same contacts web API as my Aurelia posts.

Update

If you are having issues upgrading beta 7 check out this post for a solution.

9 thoughts on “Angular 2 Quickstart with ASP.NET Core”

  1. Can you please point me to a beginner’s step by step to continue what you have here at “Gulp Tasks”?
    Just not sure where to put which files and how to run Gulp …

    Thanks in advance

  2. THANKS! This is so far the only beginner’s tutorial I can find to combine Angular2 and ASP.NET Core…

    However, I have 2 questions still puzzled me:

    1. When update the angular 2 to latest version “2.0.0-beta.7” , the build process failed. Many d.ts files throws errors complaining they cannot find “Promise” and “Map” and etc. Any suggestion?

    2. What should I do to compile “app.component.ts” and “boot.ts” into js? Or should Visual Studio automatically do this for me in each build?

    Thanks very much.

      1. Thanks for the quick and great reply ! Beginners (like me) are always eager to keep their lib version to the latest so that they don’t miss any fun new stuff, you know that…

  3. Fiddler is a very useful tool to show 404s and find missing files / bad references.
    The browser does it too, but sometimes I find it easier with an external tool.

Leave a Reply

Your email address will not be published. Required fields are marked *