Create an Angular 7 App with ASP.NET Core 2.1

This step by step getting started tutorial explains how to build an Angular 7 Web application by using Angular 7 Web Application (.NET Core) Template and ASP.NET Core MVC Application in Visual Studio 2017.

Introduction

This tutorial will guide you and help you through getting started with Angular 7 and ASP.NET Core 2.1. In this tutorial, we use Angular 7 Web Application (.NET Core) Template in an ASP.NET Core MVC Application. We will also learn how to work with two new features added to Angular 7, i.e., Virtual Scrolling and Drag and Drop Items.

If you are new to Angular, please read my previous articles from the following links:

Getting Started With Angular 7 And ASP.NET Core 2.1 

Prerequisites

Make sure you have installed all the prerequisites on your computer. If not, then download and install all of the following tools in the same order.
  1. First, download and install Visual Studio 2017 from this link. Visual Studio Community version is free. You can also use any later versions including Visual Studio 2019. 
  2. Download and install .NET Core 2.0 or later versions.
  3. Download and install the latest version of Node.js from this download link.

Angular 7 Web Application (.NET Core) using Template

 
Now, it’s time to create our first ASP.NET Core and Angular 7 application using the template. 

Step 1. Create Angular 7 ASP.NET Core using Template

After installing all the prerequisites listed above, click Start >> Programs >> Visual Studio 2017 >> Visual Studio 2017 on your desktop.

Click New >> Project. Select Online >> Template >> Search for Angular 7 .NetCore 2 Template.

Getting Started With Angular 7 And ASP.NET Core 2.1 

Download and install the template.

Getting Started With Angular 7 And ASP.NET Core 2.1 

We can see that the new Angular 7 Web Application (.NET Core) template has been added. Select the template, add your project name, and click OK to create your Angular 7 application using ASP.NET Core.

Getting Started With Angular 7 And ASP.NET Core 2.1 

You can see that a new Angular 7 project has been created. Also, we can see the ASP.NET Core Controller and Angular 7 Client App folder from the Solution Explorer.

Getting Started With Angular 7 And ASP.NET Core 2.1 

If we open the package.json file, we can see the new Angular 7 package has been installed to our project.

Getting Started With Angular 7 And ASP.NET Core 2.1 


Angular CLI

 
We need to upgrade our Angular CLI to version 7. If you have not yet installed the Angular CLI, then first install Angular CLI and upgrade to Angular CLI version 7.

Now, let’s start working with the Angular part.

First, we need to install the Angular CLI to our project.  

Angular CLI is a command line interface to scaffold and build Angular apps using node.js style (commonJS) modules. For more details click here 

To install the Angular CLI to your project, open the Visual Studio Command Prompt and run the below command.

npm i -g @angular/cli

Step 2 - Build and Run the Application

Now, our application is ready to build and run. To see the sample Angular 7 page, once we run the application, we can see a sample Angular 7 page that looks like below.

Getting Started With Angular 7 And ASP.NET Core 2.1 

ClientApp folder

Our Angular files are in the ClientApp folder. If we want to work with a component or an HTML page, we open the app folder in ClientApp and we can see app.Component.ts and app.Component.html.

Getting Started With Angular 7 And ASP.NET Core 2.1 

Now we can change the title from our component file and display a new subtitle with a datetime in our app HTML page.

In our app.Component.ts file, we changed the default title and also added a new variable to get the current date and time to display on our html page.

  1. title = 'Welcome to Shanu Angular 7 Web page';  
  2. subtitle = '.NET Core + Angular CLI v7 + Bootstrap & FontAwesome + Swagger Template';   
  3. datetime = Date.now();  
Getting Started With Angular 7 And ASP.NET Core 2.1 

In our html page, we bind newly declared variable datetime as shown in the following code:

  1. <h1>{{title}}</h1>  
  2.      <h3>{{subtitle}}</h3>  
  3.      <h4>  
  4.     Current Date and Time:   {{datetime | date:'yyyy-MM-dd hh:mm'}}  
  5. </h4>  

When we run the application, we can see the title has been updated and it displays today's date and time. See the below image.

 
Getting Started With Angular 7 And ASP.NET Core 2.1 
 

Using ASP.NET Core Web Application

We can also use ASP.NET Core Web Application template to build an Angular 7 app.

Step 1. Create ASP.NET Core Web Application

Launch Visual Studio 2017.

Click New >> Project. Select Web >> ASP.NET Core Web Application. Enter your project name and click OK.
 
Getting Started With Angular 7 And ASP.NET Core 2.1 

Select Angular Project and click OK.

Getting Started With Angular 7 And ASP.NET Core 2.1 

Step 2. Upgrade to Angular 7

By default, we can see the Angular 5 version has been installed in our project. We can check this from our Package.json file. 

Getting Started With Angular 7 And ASP.NET Core 2.1

 

For upgrading to Angular 7, first we delete the ClientApp folder from project and create a new ClientApp from the Command prompt.

First, we delete the ClientApp folder from our project.

Getting Started With Angular 7 And ASP.NET Core 2.1 

To install and create a new ClientApp with Angular7 packages, open a command prompt and go to our project folder, enter the below command and run to install the Angular 7 Packages. This action will create a new ClientApp folder.

>> ng new ClientApp

Getting Started With Angular 7 And ASP.NET Core 2.1 

It will take few seconds to install all the Angular 7 Packages. You may notice the command line activity of installing packages.

Getting Started With Angular 7 And ASP.NET Core 2.1 

We can see a new ClientApp folder has been created in our project. When we open Package.json file, the Angular 7 Version has been installed in our project.

Getting Started With Angular 7 And ASP.NET Core 2.1 

Note
We need to upgrade our Angular CLI to version 7. If you have not yet installed the Angular CLI then first install the Angular CLI and upgrade to Angular CLI version 7.

Now, let’s start working with the Angular part.

First, we need to install the Angular CLI to our project

Angular CLI

Angular CLI is a command line interface to scaffold and build Angular apps using node.js style (commonJS) modules. For more details click here

To install the Angular CLI to your project, open the Visual Studio Command Prompt and run the below command.

npm i -g @angular/cli

Step 3. Build and Run the Application

Now our application is ready to build and run. To see the sample Angular 7 page, run your application. You will see a page that looks like the following image.

Getting Started With Angular 7 And ASP.NET Core 2.1 

What’s new in Angular 7

Virtual Scrolling and Drag and Drop are two major features added to Angular 7 CDK. If we have a large number of items in a list and want fast performance scrolling to load and display its items, we can use the Virtual Scrolling feature. Angular 7 Drag and Drop feature allows us to drag and drop items of list to same list or to another list. 

Let's see how these two features work in a real working app.
 

Installing Angular CDK

To implement Virtual Scrolling and Drag and Drop features in our Angular 7 app, we need to install Angular CDK package.

To do so, open command prompt and go to our project's ClientApp Folder path and run the following command.
 
Getting Started With Angular 7 And ASP.NET Core 2.1 

You'll see the package are being installed with success messages. Angular CDK packages have been added to our project.

Getting Started With Angular 7 And ASP.NET Core 2.1 
 

Virtual Scrolling

App Module - Importing the Scrolling Module to our App

In order to work with Virtual Scrolling feature, we need to import the ScrollingModule to our Modules app.

Open our Module.ts file.

Here we will be working with our default app.module.ts to import ScrollingModule to implement virtual scrolling in our application.

Add the below code in the import section of your module to import the ScrollingModule.

Getting Started With Angular 7 And ASP.NET Core 2.1 

  1. import { ScrollingModule } from '@angular/cdk/scrolling';  

Also, we need to add the import section that adds the ScrollingModule.

  1.   imports: [  
  2.     BrowserModule,  
  3.     AppRoutingModule,  
  4.     ScrollingModule  
  5. ],  

Our new code looks like the following:

Getting Started With Angular 7 And ASP.NET Core 2.1 

App Component

To display items, we need a list of items.

We're going to create an array of items. The Item variable represents an item of the list. An array is a collection of items and will be used as the data source to display in the list.
 
Open the app.component.ts file and add the below code in your component export class.
  1. incrementValue: number[] = [];  
  2.   constructor() {  
  3.     for (let index = 1; index <= 200; index++) {  
  4.       this.incrementValue.push(index);  
  5.     }  

The complete code will look like this. As you can see from the below code, we have a component with variables, title, subtitle, and datetime. Our constructor creates 200 items,. 

Getting Started With Angular 7 And ASP.NET Core 2.1 

Add CSS

For our List scrolling, we will format the list using CSS to provide additional formatting including rounded corners and colors.

Add the below CSS code to your app.component.css file:
  1. ul {    
  2.   max-width800px;    
  3.   color#cc4871;    
  4.   margin20px auto;    
  5.   padding2px;    
  6. }    
  7.     
  8. .list li {    
  9.   padding20px;    
  10.   background#f8d8f2;    
  11.   border-radius: 12px;    
  12.   margin-bottom12px;    
  13.   text-aligncenter;    
  14.   font-size12px;     
  15. }    

Design your HTML page to display the List with CDK Virtual Scrolling

Now it's time to design our html page that will implement Virtual Scrolling functionality to a List of items.

Open app.component.html and add the below code to display the list with Virtual Scrolling enabled.
 
Inside the list, we use the cdk-virtual-scroll-viewport to add the virtual scrolling to our list. Here we also set the width and the height of the List with the Item size per scroll.
  1. <h2>Angular 7 Virtual Scrolling </h2>  
  2. <hr />  
  3.   <ul class="list">  
  4.     <cdk-virtual-scroll-viewport style="width:200px;height: 300px" itemSize="5">  
  5.       <ng-container *cdkVirtualFor="let incValue of incrementValue">  
  6.         <li> Loop {{incValue}}  </li>  
  7.       </ng-container>  
  8.     </cdk-virtual-scroll-viewport>  
  9.   </ul>  
  10.   <router-outlet></router-outlet>  

Now build and run the application.

The outlook looks like the following where you can see there is a list of 200 items and virtual scrolling lets you scroll the items.
 
Getting Started With Angular 7 And ASP.NET Core 2.1

Implement Drag and Drop in Angular 7

 
Now, let's see how we can add the drag and drop functionality to our app by using Angular 7's new Drag and Drop feature. 
 
Add DragDrop Module to the App

In order to work with Drag and Drop after adding the CDK project, we need to import the DragDrop Module to our Modules app.

Open our app.Module.ts file. Here we will work with our default app.module.ts to import the DragDrop Module to create our Drag and Drop items in our application.

Getting Started With Angular 7 And ASP.NET Core 2.1 

Add the below code in the import section of the module.

  1. import { DragDropModule } from '@angular/cdk/drag-drop';  

Also we need to add the import section and add the ScrollingModule to support virtual scrolling.

  1. imports: [  
  2.   BrowserModule,  
  3.   AppRoutingModule,  
  4.   ScrollingModule ,  
  5.   DragDropModule  

Our new code looks like the following image.

Getting Started With Angular 7 And ASP.NET Core 2.1 

App Component

For adding items to the list, we need an Item. For creating the Item in our app component, we create a new array and add items to the array in the constructor. By this when the page loads the new array Item will be created with new values.

Open the app.component.ts file and add the below code in your component export class.
  1. incrementValue: number[] = [];  
  2.   
  3.   decrementValue: number[] = [];  
  4.   constructor() {  
  5.     for (let index = 1; index <= 200; index++) {  
  6.       this.incrementValue.push(index);  
  7.     }  
  8.   
  9.     for (let int1 = 400; int1 >= 201; int1--) {  
  10.       this.decrementValue.push(int1);  
  11.     }  
  12.   }  
  13.   
  14.   drop(event: CdkDragDrop<string[]>) {  
  15.     moveItemInArray(this.decrementValue, event.previousIndex, event.currentIndex);  
  16.   }  
  17. }  

Now, we need to Import the CdkDragDrop with MoveItemInArray to create the Drop event. The Drop event is fired when an item is dragged and dropped to the list at a selected position.

  1. import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';  

Then we add the drop event method inside our app component class for adding the selected item array to the selected current index.

  1. drop(event: CdkDragDrop<string[]>) {  
  2.     moveItemInArray(this.decrementValue, event.previousIndex, event.currentIndex);  
  3.   }  

The complete code looks like the following:

Getting Started With Angular 7 And ASP.NET Core 2.1 

Add CSS

For our List Drag Drop functionality, we add the below CSS to design our list.

Add the below CSS code to your app.component.css file
  1. .divClasslist {  
  2.   width200px;  
  3.   bordersolid 1px #234365;  
  4.   min-height60px;  
  5.   displayblock;  
  6.   background#cc4871;  
  7.   border-radius: 12px;  
  8.   margin-bottom12px;  
  9.   overflowhidden;   
  10. }  
  11. .divClass {  
  12.   padding20px 10px;  
  13.   border-bottomsolid 1px #ccc;  
  14.   color: rgba(0000.87);  
  15.   display: flex;  
  16.   flex-direction: row;  
  17.   align-items: center;  
  18.   justify-content: space-between;  
  19.   box-sizing: border-box;  
  20.   cursormove;  
  21.   background#f8d8f2;  
  22.   font-size14px;  
  23. }  
  24.   .divClass:active {  
  25.     background-color#cc4871;  
  26.   }  

Design your HTML page to display the List with CDK Virtual Scrolling

Now it’s time to design our HTML page that will have the Drag and Drop functionality available in the List.

Open app.component.html and add the below code to display the item in list with Drag and Drop features added.

Here we create the cdkDropList div element with Drop event using cdkDropListDropped. We add one more div elevement inside the cdkDroplist for adding the item with cdkDrag features for dragging the item inside the selected div element.

  1. <h2>Angular 7 Drag and Drop </h2>  
  2. <hr />  
  3.    <div cdkDropList class="divClasslist" (cdkDropListDropped)="drop($event)">  
  4.      <div class="divClass"  *ngFor="let decValue of decrementValue" cdkDrag>{{decValue}}</div>  
  5.    </div>  
  6. <router-outlet></router-outlet>  

Now, build and run your application. The working output looks like the following. You can now drag and drop the list items to any position you like.

 

Getting Started With Angular 7 And ASP.NET Core 2.1

 

Conclusion

In this step by step tutorial, you learned how to build an Angular 7 Web app using Visual Studio. We also reviewed the newest features of Angular 7, virtual scrolling and drag and drop. In the end, we saw working demos of these features.

 
I hope you liked this article. In my next article, I will cover how to perform CRUD operations using Angular 7 in an ASP.NET Core app with the help of Web API.