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 Angular. 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 web application with typescript, having an api where I can call to build with ASP.NET Core. We will also use Visual Studio Code to get the client-side programming going.
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 ;-)
You will get an empty solution with no dependencies so far. Lets go and add some: open up the “project.json” and add 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.
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 :)
To get started with our web application we need an index.html. Lets add it and put nothing in it so far.
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 “dotnet run” in it. This will start a webserver and serve all files in wwwroot.
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.
If we now run the server again with “dotnet run” it will start and via Postman we can check if everything works correctly:
Great. So lets start clientside. What we need is an Angular 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.
As we start developing Angular 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 Angular case.
Lets add a file for configuring the typescript-options for our project: tsconfig.json to the root of our project.
Angular is served via npm. To get Angular we have to add a package.json to the root of our project. “npm start” will run the lite-server, compile the *.ts files and start the watcher :)
or just type “npm init” to answer all questions and get an project.json file generated.
After this you can install Angular 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.
Now we have to configure the system.js as so:
Now go ahead in you index.html and link to the files in the client js folder:
We are done so far:
You can now go ahead and develop you application in Typescript and Angular. You could start with the Angular quickstart. All files you need should be there. Its important to add the main.ts , the app.component.ts and app.module.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 “npm run 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
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. I have done that all in my example repository here on Github. Hope you like it.