Lately, I have been spending time learning Angular 2. During my study, I have seen many examples of setting up and getting started by using Visual Studio Code, WebStorm, and other excellent IDE’s. I have also read several articles about setting up and using Angular 2 beta versions with Visual Studio 201x. Since I spend most of my time using Visual Studio 2015, this article will show you how to setup Angular 2 RC5 with Visual Studio 2015 and ASP.NET Core 1.0 on .NET Core 1.0.

Before you get stared, make sure you have Update 3 for Visual Studio 2015 installed and Preview 2 tooling for Microsoft .NET Core 1.0. Once you have confirmed your installation, fire up Visual Studio 2015 and select “New Project” from the Start Page. You will be presented with the dialog below:

Make sure you have selected the “ASP.NET Core Web Application (.NET Core)” option. Name the project and the solution, and set the location to whatever you would like. Click “Ok” to continue. Next you are presented with selecting what type of project template you would like to begin with. Select “Empty”. Authentication should be set to “No Authentication” and the “Host in the cloud” option should not be selected. See the screenshot below:

Click “Ok” to continue. After VS (Visual Studio) completes the setup you will be presented with the project readme HTML file in the editor. If you take a look at Solution Explorer, your solution structure should look like the screenshot below:

The next step in setting up ASP.NET Core 1.0 to serve Angular 2 RC5 is to configure your application to serve static files. First, right-click on your web application in solution explorer and select “Manage NuGet Packages”. In the NuGet Package Manager, enter “Microsoft.AspNetCore.StaticFiles” in the search window. You will be presented with the screenshot below:

Make sure you have the latest stable version selected and click “Install”. You may be asked permission to update your application as well as accept licensing terms. Confirm to complete the installation and close the NuGet Package Manager Tab. Finally edit the Startup.cs file in your solution. It should look like the code below:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace WebApplication1
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseFileServer();
}
}
}

For a more in depth understanding of serving static files with ASP.NET Core 1.0, please visit here.

Now we are ready to begin setting up our environment for Angular 2 RC5. “Right-click” on your web application project and select “Add” then “New Item”. You will be presented with the dialog below:

Navigate to “Client-side” under .NET Core and then select “npm Configuration File” (package.json). Click Add. This file is used by NPM (Node Package Manager) to install required modules for our Angular 2 application. You will be presented with the package.json file loaded in the editor. Edit the package.json file to look like the code below:

Immediately after saving the package.json file, Visual Studio will begin downloading all the dependencies listed to a folder named “node_modules” in the directory where your web application is located. (If you would like a detail explanation of these settings, you can go here.) In addition, you will probably receive an error message stating that npm was not able to resolve Typings dependencies due to a missing “typings.json” file. Let’s create that file now.

“Right-click” on the web application and select “Add” then “New Item”. Again select “Client-side” on the far left and then select “JavaScript File”. Make sure you name the file “typings.json”. Your screen should look like the screenshot below:

Click “Add” and you will be presented with the typings.json file in the editor. Edit the typings.json file to look like the code below:

Save the typings.json file. If you would like an explanation of these settings, you can go here. Now if you switch to the project.json file and save it again, Visual Studio should complete the installation of the dependent modules without error.

Next we add a TypeScript JSON Configuration File. (tsconfig.json) “Right-click” on the web application in Solution Explorer and select “Add” and then “New Item”. Select “Client-side” on the left and “TypeScript JSON Configuration File”. Your screen should look like the screenshot below:

Select “Add” and the tsconfig.json file will be loaded into the editor. Edit the tsconfig.json to look like the code below:

Save the file. If you would like an explanation of these settings, you can go here.

We are going to use SystemJS to load our application and library modules. To do so, we need to create a configuration file for SystemJS so that it can locate the code we need loaded.

Since ASP.NET Core 1.0 serves static files from the “wwwroot” folder, we are going to place the SystemJS configuration file in a folder named “scripts” under this folder. “Right-click” on the wwwroot folder and select “Add” then “New Folder”. Name the folder “scripts”. Solution Explorer should look like the screenshot below:

Now add the SystemJS configuration file to the scripts folder by selecting “Right-click” on the script folder and selecting “Add” then “New Item”. You will be presented with the dialog below:

Select “Client-side” on the left and “JavaScript File”. Name the file “systemjs.config.js”. Click “Add” and the “systemjs.config.js” file will be displayed in the editor. Edit the “systemjs.config.js” file to look like the code below:

If you are interested in the settings in the “systemjs.config.js” file, you can go here.

There is one final configuration step to complete and then we are ready to code our Angular 2 application. As stated before, ASP.NET Core 1.0 serves static files from the wwwroot folder by default. As a result, we need to move required library files from the node_modules folder to a location under the wwwroot folder. In addition, if we would like to perform any debugging of the TypeScript code in our browser development tools, we need to have the original TypeScript files served from the server. In order to accomplish this, we are going to use a gulp script to handle copying the files to their needed location.

“Right-click” on the web application and select “Add” then “New Item”. Select “Gulp Configuration File”. The dialog should look like the screenshot below:

Click “Add” and you will be presented with gulpfile.js in the editor. Edit the gulpfile.js to look like the code below:

The gulp file is configured to execute after a successful build of the solution once the Task Runner Explorer is setup. From the VS menu, select “View”, “Other Windows”, and then “Task Runner Explorer”. Click the “Refresh” button (top left, next to the application name) and Task Runner Explorer will read the gulp file. You should see three(3) task: clearAppDestinationFolder, clearLibsDestinationFolder, and moveToLibs. You should also see the number three(3) next to the “After Build” Bindings. Now the gulp script will execute after a successfully build.

Okay, finally let’s code our Angular 2 application.

First step is to add an “index.html” file. “Right-click” on the wwwroot folder and select “Add” then “New Item” and add an HTML file named “index.html”. Edit the file to look like the code below:

Now let’s add a new folder name “app” that will contain all of our TypeScript code for our Angular application. “Right-click” on the web application project and select “Add” then “New Folder”. Name the folder “app”.

The first TypeScript file we will add to the project is “main.ts”. This is where our Angular 2 application starts up. “Right-click” on the “app” folder and select “Add” then “New Item”. Select “Client-side” on the left and in the center select “TypeScript File”. Name the file “main.ts” and select “Add”. “main.ts” should now be loaded in the editor and your solution should look similar to the screenshot below:

Visual Studio will flag a few errors but that is okay for now. They will be resolved when we add the remaining files. Now let’s add a new TypeScript file to the app folder named “app.module.ts” and edit the file to contain the code below: