Introduction
In this article, I will explain how to create SPA Application using an ASP.NET Core and Angular 2 with TypeScript.
Prerequisites
The following software needs to be installed in our system before starting the work.
- .NET Core 1.1.
- TypeScript 2.0.
- Node JS version 6 or later.
- Editor such as VS 2017 or VS Code.
The easiest way to create Angular SPA (Single Page Application) Application with an ASP.NET Core is to use project template. There are many project templates, which are are available on Nuget. To install SPA template, we need to run the command given below from the command prompt.
- dotnet new --install Microsoft.AspNetCore.SpaTemplates::*
After successful installation of the templates, it lists down all the installed templates. We can use any installed template to create SPA.
Here, I want to create SPA, using MVC ASP.NET Core with Angular, so we need to execute the command given below, using the command prompt at the place, where you want to create an Application.
After successful creation of an Application, we need to download dependencies of an Angular 2 as well as an ASP.NET Core. To download the dependencies of Angular, we need to execute the command given below, using the command prompt. This command reads package.json file and download the dependencies.
Similarly, using the command given below, we can download the dependencies of an ASP.NET Core, which is defined in package.json (VS 2015) or .csproj file (VS 2017).
Once all the project dependencies are installed and if we have an editor such as VS 2017, then we can run the project by pressing CTRL + F5.
Alternatively, we can run the project by running the command given below from the command prompt.
Output
Structure of the Project
When we look at the structure of the project, it contains the files related to configuration such as package.json, tsconfig.json and Angular project related files as well as ASP.NET Core MVC related files.
This project initially bootstraps as an ASP.NET Core MVC project by calling an index action method of Home controller. Index.cshtml file internally bootstraps an Angular component i.e. app.component.ts.
This project template puts all Angular related code in ClientApp folder. This folder also contains UI testing related configuration and the code.
Adding New Module in the project template
To add new module to this project, first we need to create new folder “MyDetails” under the ClientApp>> app>> Components folder.
Next step is to create module, component and template view.
mydetails.Component.ts
- import { Component } from '@angular/core';
-
- @Component({
- selector: 'mydetails',
- templateUrl: './mydetails.Component.html'
- })
- export class MyDetailsComponent {
-
- }
mydetails.Component.html- <h1>
- Welcome to my details page.
- </h1>
The next step is to add navigation to navMenu component. I have a