How To Setting Up VS 2015 for ASP.NET Core RTM 1.0 and Angular 2 Final/RTM ?

How To Setting Up VS 2015 for ASP.NET Core RTM 1.0 and Angular 2 Final/RTM ?

CheapASPNETHostingReview.com | Best and cheap ASP.NET Core RTM 1.0 hosting. In this post I will explain how to setup Visual Studio 2015 in order to develop an Angular 2 RC3 application hosted inside ASP.NET Core project.  I of course read the quick start first.  You need to make sure to install VS 2015, you can use Community edition of you want.  You would also need to install ASP.NET Core.

Let’s get started.  Create new ASP.NET Core project.

Select empty template to keep everything simple for getting started demo.

asp1

Let’s add static files support to serve a plain vanilla HTML page.  Open project.json file and add the static files line from below.

I also added support for DotNet.Watcher tools which make it easier to make changes to C# code and have the process automatically compile and restart as needed.

Now we can add a page which will host our Angular app.  Again, add just a plain HTML page under wwwroot folder, since that is the root of our deployable Core application.  You can just right click on that folder, add new item, then pick HTML page template.  I am calling mine index.html.  In order to see this page we need to turn off preloaded startup code and replace it with static files server.  Open up Startup.cs file and replace Configure method with the following.

Now we can test the host page.  Open up VS command prompt and navigate to the project folder (NOT solution folder).  Type “dotnet  watch run”.  Your app will compile and Kestrel web server with start.  Open browser and navigate to http://localhost:5000/index.html.  You will see the host page.  To make sure edit the index.html page and add some content to it.  It is time to start up Angular 2 portion.  I like adding NPM support to the project from command line.  From the same command prompt type “npm init” to initialize NPM file.  Follow the prompts, giving some meaningful answers.  Once this is done, switch to visual studio.  You will see that dependencies node in project explorer with have npm listed.  Right click on that node and select open package.json. We need to add all Angular 2 scripts and dependencies by adding dependencies and devDependencies nodes as shown below.

All packages that start with @angular are angular core packages.  This is different from beta versions, which only had a single package.  Other packages are supporting packages for angular 2.  In order to proving TypeScript support, we will use typings NPM module, which is under devDependencies.  Give VS a minute to restore the packages after saving package.json file.  If you get an error, just type “npm install” at the command prompt.

Now we need to install code JavaScript typings files for TypeScript.  At the same prompt type “typings install  –global –save dt~core-js”.  Once completed, you will see typings folder in project explorer, with core-js folder under it.

In the next step we will setup compilation options for TypeScript.  Add new item to the root of your project, selecting TypeScript Configuration File item template.  Change the content to look as follows.

We are adding decorator options, which are required for Angular 2.  We are setting module to use systemjs, which is the default for all the demos I have seen.

Now I am going to setup gulp to make my development flow a bit smoother.  If you do not have gulp installed globally, type “npm install gulp”  at the command prompt.  After that you can add new item to the root of the project, selecting Gulp Configuration File item template.  Open gulpfile.js and replace the content with the following.

What is going on here?  Third party task copies necessary Angular 2 scripts from node_modules folder to the same folder under wwwroot.  Copy task copies our own application files, which will be placed under app folder under project root to wwwroot folder as well.  Finally, watch task will monitor the changes to app folder and re-copy the files wwwroot folder by firing copy task.  By default we will run all three tasks when we run gulp every time.

Time to write application code.  Create new folder called “app” under project root.    Now we need to create root component.  We will add app.ts file with the following code.

We created a new class called App.  It is decorated with a component attribute.  We specify the selector or tag that will be replaced with our component.  We also specify the HTML that will be injected and bound to the component.  The component just has a property and a method to test data binding.

Now we need to create a starting module.  Let’s call the file appModule.ts

This class actually does not have any executable code, just a decorator that defines our root module.  It imports compile module, BrowserModule, necessary for compiling and bootstrapping Angular code in browser.  it contains our single App component that we both declare and bootsrtap, setting it as entry point into the application.

Finally, add new item to shell folder, picking TypeScript file template.  Change the name to main.  This will be our bootstrapping code for Angular 2 application.

Now we need to configure systemjs module loader.  Create new JavaScript file called system.config.js under wwwroot folder.  This is the only file we will add there.  We could also add it under app folder if we wanted to.  Here it its content.

We are specifying our app folder as a package for system js.  We are also adding core angular packages, including rxjs, reactive extensions.  We are configuring systemjs by calling config method and passing our packages map.  Finally we are starting app package.  Previously in the same file we specified that the entry point for it is main.js.

It is time to edit our index.chtml page and add the necessary scripts.  We will start by creating views folder with some stock files.  I am adding _ViewStart.cshtml under Views.

This code just sets up default layout page for all our views.  Now we just need default view.  I am going to create home folder and single view under it Index.cshtml.  Its content is very simple.

It just contains the tag for our root component.  Now the import part of adding out layout page, _Layout.cshtml under Views/Shared folder.  It just contains all the necessary libraries.

It also setups up configuration object, which we do not technically need in this sample app, but it is a good illustration on how to inject server side configuration into client side JavaScript.  We are keeping node_modules and third party libraries folder, but to be sure, we are not using entire folder, we just compy necessary stuff via gulp (see above).

Time to create C# controller under Controllers folder.  We will call it HomeController.

You can now build the project in the Visual Studio just in case.  Then switch to command prompt, navigate to the folder where our project is (NOT solution).  Just make sure you see project.json file.  Now we need to run gulp to copy files around.  Type “gulp” and let the process finish.  Now open second prompt, nvaigate to the same folder and type “dotnet watch run”.  This will compile if necessary, start runtime process and watch for any changes.  Let this finish as well.  Make sure you see something like the following:

Final step.   Open browser and navigate to the URL above.  You should see the following.

asp2

Feel free to click on a button to make sure the method in your App component got bound properly as well.  Also feel free to make changes to the App component and refresh the page to see them.

In this post we achieved a lot.  We setup new .Net Core project, created default controller and cshtml page, setup Angular 2 minimal application, setup watcher for .NET and TypeScript code to make changes without manual compilation and ran our project.  Quite a bit of work.