Thursday 5 May 2016

Start with VS Code, Angular2, Typescript & ASP.NET Core 1.0

Nowadays you hear a lot of new technologies around the web stuff and with this post I want to show you a get-starting-guide how to start with the following technologies in general. I want to show how to set up an environment where you can start programming ASP.NET and AngularJs 2.0. As I started I spent a lot of time setting up all my tools and stuff. If you want to save this time: Go ahead reading.

So I want to have an environment where I can build an Angular 2 web application with typescript, having an api where I can call to build with ASP.NET Core 1.0. We will also use Visual Studio Code to get the client-side programming going.

Lets start:

First thing to do is setting up our Visual Studio. Create a new application and choose the ASP.NET 5 Templates. I guess they will be renamed later ðŸ˜‰
01

02

You will get an empty solution with no dependencies so far. Lets go and add some: open up the “project.json” and add the dependencies like this:
What we are adding is MVC, because we will need it for setting up the API, some logging things and AutoMappers to map between our DTOs and our real Entities. We wont use it in this tutorial, but for a kickstart its good to have them.
Visual Studio should now go ahead and resolve, download and install these packages.
03
Now go on and add an “app”-Folder, a “js”-folder and a “css”-folder to the wwwroot-folder.
Everything in the wwwroot-folder is going to be served to the client. This is where our app lives. You can also build your application outside this folder and copy the relevant files in it via gulp etc. I am not doing this in this case because its easier to debug in the when launching. If you have a better approach I would love to hear it ðŸ™‚
04
To get started with our web application we need an index.html. Lets add it and put nothing in it so far.
05

The Serverside/API

Lets get started with the serverside: To wire up everything we need to configure our application in the Startup.cs.
Because at the beginning of this page we are consuming an “appsettings.json” we should also create it. Create a new file called like this beside the Startup.cs and paste some logging information to it. You can add some informations you want to have in there later if you want. For the start, this should be okay.
Our application should now be able to start up. Just go to the commandline and type “dnx web” in it. This will start a webserver and serve all files in wwwroot.
06
07
Great so far! Now we can add a controller!
For this go ahead in the root of the project and add a folder “Controller” which wil hold all the controllers for us. Then you can add an standardcontroller for the first run.
I like to stay to IActionResult-Interface in my controllers. It just gives me a way I am used to and is clearer for me. You do not have to do this. The controller is just for demo purposes.
08If we now run the server again with “dnx web” it will start and via Postman we can check if everything works correctly:
09

The Client


Great. So lets start clientside. What we need is an AngularJs 2.0 environment. For the tooling I made the best experiences using Visual Studio Code.
We already defined the wwwroot-Folder which holds every file which is transported to the client.
11
As we start developing AngularJs 2.0 we have to prepare our environment for working with Typescript. I already have an instruction how to start in a previous Blogpost Getting started with Visual Studio Code & Typescript. But lets cover some thing in short to get it customised for this AngularJs 2.0 case.
Lets add a file for configuring the typescript-options for our project: tsconfig.json to the root of our project.

Adding Angular2

Normally you would add AngularJs 2 via the tsd-command. But in this case AngularJs 2 is served via npm. To get Angular2 we have to add a package.json to the root of our project.
or just type “npm init” to answer all questions and get an project.json file generated.
After this you can install AngularJs 2 and all other dependencies via “npm install” or visual Studio will do this for you automatically. Just just have to wait a few seconds. It will add a node_modules folder to your root application and copy all files, also the *.d.ts-files you need to develop with typescript.
Last thing to do is copying all your needed files into the “js”-folder you created on clientside.
10(Ignore the jquery and signalr files I had addinitonally in the example, its for the next blogpost ðŸ˜‰ )
Now go ahead in you index.html and link to the files in the client js folder:

Finally…

We are done so far:
You can now go ahead and develop you application in Typescript and Angular2. You could start with the AngularJs 2 quickstart. All files you need should be there. Its important to add the boot.ts and application.ts to get an entry point and bootstrapping your application.
If you, like me, always forget to compile the typescript files: open a cmd in the app folder and just type “tsc -w”. This activates a typescript watcher using the tsconfig in the root of the project and will always compile the *.ts files into *.js files if there are some changes

Further steps:

You could now start using gulp/grunt to copy files to the locations automatically instead of doing it manually. Or you could start consuming the api using the http-Service like shown here or wait for one of my next blog-posts ðŸ˜‰

2 comments:

  1. Thanks for providing good information,Thanks for your sharing.Angularjs Online Training Bangalore

    ReplyDelete

  2. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.AngularJS Online course

    ReplyDelete

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...