ASP.NET Core1.0: Insert/Select To Database Using Angular2 And WEB API

output
Introduction

In our previous article,

we have seen in detail about ASP.NET Core 1.0, Angular2 and WEB API to display the data.

In this article we will see in detail about using ASP.NET Core 1.0 with Angular2 to Insert and Select Student Details to SQL Server Database using WEB API.

In this article we will see in detail how to,
  • Create our ASP.NET Core 1.0 RC2 Web Application.
  • Create Model to add Student details.
  • Create WEB API Controller using Entity framework to Select and Insert data to database.
  • How to add TypeScript JSON Configuration File
  • How to add grunt package using NPM configuration file
  • How to configure Grunt File.
  • Adding Dependencies to install Angular2 and all other files
  • How to create our Angular2 App, boot using Type Script file.
  • Using HTTP Get/Post to Insert and select data from Angular2 to WEB API to insert in DB.
  • Creating HTML File
  • How to Run the Grunt file using Visual Studio Task Runner Explorer
  • How to Run and view our Sample.

Reference website :

Prerequisites

  • Visual Studio 2015: You can download it from here.
  • ASP.NET Core 1.0 RC2: download link,link2.
Code Part

Create a Database

We will be using our SQL Server database for our WEB API. First we create a database named StudentsDB and a table as StudentMaster. Here is the SQL script to create Database table and sample record insert query in our table. Run the below query in your local SQL server to create Database and Table to be used in our project.
  1. USE MASTER   
  2. GO   
  3.    
  4. -- 1) Check for the Database Exists .If the database is exist then drop and create new DB   
  5. IF EXISTS (SELECT [nameFROM sys.databases WHERE [name] = 'StudentsDB' )   
  6. DROP DATABASE StudentsDB   
  7. GO   
  8.    
  9. CREATE DATABASE StudentsDB   
  10. GO   
  11.    
  12. USE StudentsDB   
  13. GO   
  14.    
  15.    
  16. -- 1) //////////// StudentMasters   
  17.    
  18. IF EXISTS ( SELECT [nameFROM sys.tables WHERE [name] = 'StudentMasters' )   
  19. DROP TABLE StudentMasters   
  20. GO   
  21.    
  22. CREATE TABLE [dbo].[StudentMasters](   
  23.         [StdID] INT IDENTITY PRIMARY KEY,   
  24.         [StdName] [varchar](100) NOT NULL,      
  25.         [Email]  [varchar](100) NOT NULL,      
  26.         [Phone]  [varchar](20) NOT NULL,      
  27.         [Address]  [varchar](200) NOT NULL   
  28. )   
  29.    
  30. -- insert sample data to Student Master table   
  31. INSERT INTO [StudentMasters]   ([StdName],[Email],[Phone],[Address])   
  32.      VALUES ('Shanu','syedshanumcain@gmail.com','01030550007','Madurai,India')   
  33.    
  34. INSERT INTO [StudentMasters]   ([StdName],[Email],[Phone],[Address])   
  35.      VALUES ('Afraz','Afraz@afrazmail.com','01030550006','Madurai,India')   
  36.         
  37. INSERT INTO [StudentMasters]   ([StdName],[Email],[Phone],[Address])   
  38.      VALUES ('Afreen','Afreen@afreenmail.com','01030550005','Madurai,India')   
  39.         
  40.         
  41.      select * from [StudentMasters]  
Step 1: Create our ASP.NET Core 1.0 Web Application.

After installing both Visual Studio 2015 and ASP.NET Core 1.0 RC2. Click Start, then Programs and select Visual Studio 2015 - Click Visual Studio 2015. Click New, then Project, select Web and select ASP.NET Core Web Application. Enter your Project Name and click OK.

ASP.NET Core 1.0 Web Application

Next select Web Application. If you are not hosting in Cloud then you can uncheck the Host in the Cloud at the bottom right corner. Click OK.

Web Application

Now we have,

Web Application

Database Connection String

Now we need to change the local connection string from ASP.Net 5 project with our SQL Server connection.

Note: In ASP.NET Core 1.0 we need to use “appsettings.json” file instead of web.config.We can find the “appsettings.json” file from our ASP.NET Core 1.0 solution.

appsettings.json
To change the default connection string with our SQL connection open the “appsettings.json” file .Yes this is JSON file and this file looks like below Image by default.

appsettings.json

Now the default connectionstring will be something like this,
  1. "ConnectionStrings": {  
  2. "DefaultConnection""Server=(localdb)\\mssqllocaldb;Database=aspnet-Core1.0Angular2CRUDInsert-599f2ca4-119a-4fd5-a2a4-e58fb4b2e5be;Trusted_Connection=True;MultipleActiveResultSets=true"  
  3. },  
Now we change this to our SQL Connection like below,
  1. "ConnectionStrings": {  
  2. "DefaultConnection""Server=SQLSERVERNAME;Database=StudentsDB;user id=SQLID;password=SQLPWD;Trusted_Connection=True;MultipleActiveResultSets=true;"  
  3. },  
Step 2: Creating MVC Model to return student list

We can create a model by adding a new class file in our Model Folder.

create

Right click the Models folder and click new Item. Select Class and enter your class name as “StudentMasters.cs”

StudentMasters.cs

Now in this class we first create property variable, add student. We will be using this in our WEB API controller. 
  1. using System.ComponentModel.DataAnnotations;  
  2. namespace WebApplicationTestSample.Models  
  3. {  
  4.     public class StudentMasters  
  5.     {  
  6.         [Key]  
  7.         public int StdID { getset; }  
  8.   
  9.         [Required]  
  10.         [Display(Name = "Name")]  
  11.         public string StdName { getset; }  
  12.   
  13.         [Required]  
  14.         [Display(Name = "Email")]  
  15.         public string Email { getset; }  
  16.   
  17.         [Required]  
  18.         [Display(Name = "Phone")]  
  19.         public string Phone { getset; }  
  20.   
  21.         public string Address { getset; }  
  22.   
  23.     }  
  24. }  
Step 3: Creating WEB API Controller

We can create a controller by adding a new Empty API Controller in our Controller Folder.

controller

Right click the Controller folder and Add Controller. Select API Controller with action, using Entity Framework and click Add,

Add

Select the Model class as StudentMasters.

Model class

After selecting the Model Class select the Data Context Class. Here we are selecting the default ApplicationDbContext. Note we have set the default connection string to our SQL Server database Connection. Next give our WEB API controller name as StudentMastersAPI.

add

We can see that our WEB API Controller has been created now and also the controller will contain default Select (HttpGet) and Insert method as (HttpPost). We will be using this get and post method In our Angular2 to select and insert data to database.

code

To test it we can run our project and copy the get method api path here we can see our API path for get is api/StudentMastersAPI/

Run the program and paste the above API path to test our output.

output

Step 4: How to add TypeScript JSON Configuration File

The TypeScript JSON file specifies the root files and the compiler options required to compile the project .To know more about TypeScript JSON Configuration kindly visit this site.

To create the TypeScript JSON file right click on you Project and Click Add new Item.

TypeScript JSON file

Select TypeScript JSCON Configuration File and Click ok. The File will be look like below image.

code

Now copy the below code and replace with your config file.
  1. "compilerOptions": {  
  2.     "emitDecoratorMetadata"true,  
  3.     "experimentalDecorators"true,  
  4.     "module""commonjs",  
  5.     "moduleResolution""node",  
  6.     "noImplicitAny"false,  
  7.     "noEmitOnError"true,  
  8.     "removeComments"false,  
  9.     "target""es5",  
  10.  "sourceMap"true  
code

Step 5: How to add grunt package using NPM configuration file

Now we need to add a NPM Configuration File for adding a grunt package to run our java scripts. As we have created as a Web Application the NPM Configuration File will be located in our project. By default we can’t view our NPM Configuration File. The NPM Configuration File will be in the name of “package.JSON” . To view that from the Solution Explorer click on “Show All Files”

Now open the “package.JSON” file. Now first we need to change the name to our project Solution name and add our grunt package. We can see the code here below the image.

package.JSON

Here we have changed the name as our Solution name and also added the grunt package.
  1. {  
  2.   "name""test",  
  3.   "version""0.0.0",  
  4.   "private"true,  
  5. "devDependencies": {  
  6.     "grunt""1.0.1",  
  7.     "grunt-contrib-copy""1.0.0",  
  8.     "grunt-contrib-uglify""1.0.1",  
  9.     "grunt-contrib-watch""1.0.0",  
  10.     "grunt-ts""5.5.1",  
  11.     "gulp""3.8.11",  
  12.     "gulp-concat""2.5.2",  
  13.     "gulp-cssmin""0.1.7",  
  14.     "gulp-uglify""1.2.0",  
  15.     "rimraf""2.2.8"  
  16.   }  
  17. }  
Now save the package.json file and you should be able to see a grunt package under Dependencies/ npm Folder will be first Resorted and then installed.

Restoring 

Restoring

Al Installed

Al Installed

Step 6: Configure Grunt File.

Grunt is used to build all our client side resources like JavaScript for our project.

First step is to add a Grunt file to our project. Right click our project and Select Grunt Configuration File and click Add.

Add

After creating the file now we need to edit this file to add load plugins, configure plugins and define tasks

Here in our Grunt file we have first load plugins which we have added in our npm. Using loadNpmTask here we load 'grunt-contrib-copy ,'grunt-contrib-uglify' , 'grunt-contrib-watch',

Grunt

Next we configure the grunt add the app.js file in our wwwroot folder. All our Script files from Scripts folder result will be added in this app.js file. Next we need to copy all the Script file from 'node_modules/ to our local js Folder.

The watch plugin will be used to check for any changes on JavaScript file and update it app.js with all new changes.
  1. /* 
  2. This file in the main entry point for defining grunt tasks and using grunt plugins. 
  3. Click here to learn more. http://go.microsoft.com/fwlink/?LinkID=513275&clcid=0x409 
  4. */  
  5. module.exports = function (grunt) {  
  6.     grunt.loadNpmTasks('grunt-contrib-copy');  
  7.     grunt.loadNpmTasks('grunt-contrib-uglify');  
  8.     grunt.loadNpmTasks('grunt-contrib-watch');  
  9.     grunt.loadNpmTasks('grunt-ts');  
  10.     grunt.initConfig({  
  11.         ts:  
  12.         {  
  13.             base:  
  14.             {  
  15.                 src: ['Scripts/app/boot.ts''Scripts/app/**/*.ts'],  
  16.                 outDir: 'wwwroot/app',  
  17.                 tsconfig: './tsconfig.json'  
  18.             }  
  19.         },  
  20.         uglify:  
  21.         {  
  22.             my_target:  
  23.             {  
  24.                 files: [{  
  25.                     expand: true,  
  26.                     cwd: 'wwwroot/app',  
  27.                     src: ['**/*.js'],  
  28.                     dest: 'wwwroot/app'  
  29.                 }]  
  30.             },  
  31.             options:  
  32.             {  
  33.                 sourceMap: true  
  34.             }  
  35.         },  
  36.         // Copy all JS files from external libraries and required NPM packages to wwwroot/js    
  37.         copy: {  
  38.             main: {  
  39.                 files:  
  40.                      [{  
  41.                          expand: true,  
  42.                          flatten: true,  
  43.                          src: ['Scripts/js/**/*.js''node_modules/es6-shim/es6-shim.min.js''node_modules/systemjs/dist/system-polyfills.js''node_modules/angular2/bundles/angular2-polyfills.js''node_modules/systemjs/dist/system.src.js''node_modules/rxjs/bundles/Rx.js''node_modules/angular2/bundles/angular2.dev.js'],  
  44.                          dest: 'wwwroot/js/',  
  45.                          filter: 'isFile'  
  46.                      }]  
  47.             }  
  48.         },  
  49.         // Watch specified files and define what to do upon file changes    
  50.         watch: {  
  51.             scripts: {  
  52.                 files: ['Scripts/**/*.js'],  
  53.                 tasks: ['ts''uglify''copy']  
  54.             }  
  55.         }  
  56.     });  
  57.     // Define the default task so it will launch all other tasks    
  58.     grunt.registerTask('default', ['ts''uglify''copy''watch']);  
  59. };  
Step 7: Adding Dependencies to install Angular2 and all other files

We are using NPM to install our Angular2 in our web application. Open our Package.JSON file and the below dependencies.
  1. "dependencies": {  
  2.     "@angular/http""2.0.0-rc.1",  
  3.     "angular2""^2.0.0-beta.8",  
  4.     "angular2-jwt""0.1.16",  
  5.     "angular2-meteor""0.5.5",  
  6.     "cors""2.7.1",  
  7.     "systemjs""0.19.22",  
  8.     "es6-promise""^3.0.2",  
  9.     "es6-shim""^0.33.3",  
  10.     "reflect-metadata""0.1.2",  
  11.     "rxjs""5.0.0-beta.2",  
  12.     "tsd""^0.6.5",  
  13.     "zone.js""0.5.15"  
  14.   },  
The edited Package.json file will be look like below image.

Package.json

Save the file and wait for a few second to complete all dependencies installed.

file

Step 8 How to create our Angular2 App, boot using Type Script file.

Now it’s time to create our Angular2 application. First create a Folder named Scripts. Right click our project and click add new Folder and name the folder name as “Scripts”. Now we will create our TypeScript files inside this Scripts folder.

To work with Angular2 we need to create 2 important TypeScript files. 
  1. Component File where we write our Angular coding.
  2. Boot file: To run our component app .

Creating App TypeScript file

Right Click on Scripts folder and click on add new Item. Select TypeScript File and name the file as App.ts and click Add.

Add

In App.ts file we have three parts first is the,

  1. import part
  2. Next is component part
  3. Next we have the class for writing our business logics.

Here we can see a simple basic one way binding example to display the welcome message in side h1 tag.Here.

  1. Import part

    First we import angular2 files to be used in our component here we import http for using http client in our Angular2 component and importing NgFor for using the looping and binding all the student details array values one by one, and also we are importing one more typescript export class name StudentMasters from the model file.
    1. import { Component, Injectable, Inject} from 'angular2/core';  
    2. import { NgIf, NgFor } from 'angular2/common';  
    3. import {Http, Response, HTTP_PROVIDERS, Headers, RequestOptions} from 'angular2/http';  
    4. import { StudentMasters } from './model';  
    5. import {Observable} from 'rxjs/Observable';  
    6. import 'rxjs/Rx';  
  2. Next is component part

    In component we have selector, directives and template. Selector is to give a name for this app and in our html file we use this selector name to display in our html page.

    In template we write our code. In template first we create a form to insert Student details to database using the http post method. In Button click we call the addStudentsDetails() method to insert student details. After inserting we call the getData() method to bind the updated result.

    To display the StudentMaster details we are using ngFOR inside html table. In the method we have used http get function to load data from WEB API and to display in our html page. We have made all the design inside the template. There is also another method as we can create a html file and add all the design in that html page and in template we can give the html file name.
    1. @Component({  
    2.     selector: "my-app",  
    3.     directives: [NgFor],  
    4.     template: `       
    5. <table style="background-color:#FFFFFF; border: dashed 3px #6D7B8D; padding :5px;width :99%;table-layout:fixed;" cellpadding="2" cellspacing="2">  
    6.                     <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid 1px #659EC7;">  
    7.                       
    8.                   <td>  
    9.                       <h2>Insert Student Details : </h2>  
    10.                   </td>  
    11.             </tr>  
    12.         <tr>  
    13.         <td>  
    14.  <table style="color:#9F000F;font-size:large" cellpadding="4" cellspacing="6">  
    15.  <tr>  
    16.      <td><b>Students ID: </b> </td>  
    17.     <td>  
    18.       <input  [(ngModel)]="students.StdID" value="0" style="background-color:tan" readonly>  
    19.   </td>  
    20.   <td><b>Students Name: </b> </td>  
    21.     <td>  
    22.     <input  [(ngModel)]="students.StdName" >  
    23.   </td>  
    24. </tr>  
    25.  <tr>  
    26.      <td><b>Email: </b> </td>  
    27.     <td>  
    28.       <input  [(ngModel)]="students.Email" >  
    29.   </td>  
    30.   <td><b>Phone: </b> </td>  
    31.     <td>  
    32.     <input  [(ngModel)]="students.Phone" >  
    33.   </td>  
    34. </tr>  
    35. <tr>  
    36.      <td><b>Address: </b> </td>  
    37.     <td>  
    38.      <input  [(ngModel)]="students.Address" >  
    39.   </td>  
    40.   <td colspan="2">  
    41. <button (click)=addStudentsDetails() style="background-color:#334668;color:#FFFFFF;font-size:large;width:200px;  
    42.                               border-color:#a2aabe;border-style:dashed;border-width:2px;">Save</button>  
    43.    </td>  
    44.    </tr>  
    45.    </table>  
    46.  </td>  
    47. </tr>  
    48.   
    49. <tr><td>  </td></tr>  
    50.   
    51.  <tr>  
    52.         <td>  
    53.  <table style="background-color:#FFFFFF; border solid 2px #6D7B8D; padding 5px;width 99%;table-layout:fixed;" cellpadding="2" cellspacing="2">  
    54.   
    55.                                 <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid 1px #659EC7;">  
    56.                                     <td width="100" align="center">Student ID</td>  
    57.                                     <td width="240" align="center">Student Name</td>  
    58.                                     <td width="240" align="center">Email</td>  
    59.                                     <td width="120" align="center">Phone</td>  
    60.                                     <td width="340" align="center">Address</td>  
    61.                                       
    62.   
    63.                                 </tr>  
    64.                                 <tbody *ngFor="let std of student">  
    65.                                     <tr>  
    66.   
    67.                                         <td align="center" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
    68.                                             <span style="color:#9F000F">{{std.StdID}}</span>  
    69.                                         </td>  
    70.   
    71.                                         <td align="left" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
    72.                                             <span style="color:#9F000F">{{std.StdName}}</span>  
    73.                                         </td>  
    74.   
    75.                                         <td align="left" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
    76.                                             <span style="color:#9F000F">{{std.Email}}</span>  
    77.                                         </td>  
    78.   
    79.                                         <td align="center" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
    80.                                             <span style="color:#9F000F">{{std.Phone}}</span>  
    81.                                         </td>  
    82.   
    83.                                         <td align="left" style="border: solid 1px #659EC7; padding: 5px;table-layout:fixed;">  
    84.                                             <span style="color:#9F000F">{{std.Address}}</span>  
    85.                                         </td>  
    86.                                         
    87.                                     </tr>  
    88.                                 </tbody>  
    89.                             </table>  
    90. </td>  
    91. </tr>  
    92. </table>  
    93.               `,  
    94.   
    95. })  
  3. Export Class

    This is the main class where we do all our business logic and variable declaration to be used in our component template. In this class we have created a method named getData() and in this method we get the API method result and bind the result to the student array. In button click we call the addStudentsDetails() method to insert the data to database using http post method.
    1.  export class AppComponent {  
    2.     student: Array<StudentMasters> = [];   
    3.     students = {};  
    4.     myName: string;    
    5.     constructor(@Inject(Http) public http: Http) {  
    6.         this.myName = "Shanu";        
    7.         this.getData();         
    8.     }  
    9.   
    10.     getData() {           
    11.         this.http.get('api/StudentMastersAPI')  
    12.             .map((responseData) => {  
    13.                 return responseData.json();  
    14.             })  
    15.             .map((student: Array<any>) => {  
    16.                 let result: Array<StudentMasters> = [];  
    17.                 if (student) {  
    18.                     student.forEach((student) => {  
    19.                         result.push(new StudentMasters(student.StdID, student.StdName,  
    20.                             student.Email, student.Phone, student.Address));  
    21.                     });  
    22.                 }  
    23.                 return result;  
    24.             })   
    25.             .subscribe(res => this.student = res);  
    26.     }  
    27.   
    28.     addStudentsDetails() {       
    29.         var headers = new Headers();  
    30.         headers.append('Content-Type''application/json; charset=utf-8');  
    31.         this.http.post('api/StudentMastersAPI', JSON.stringify(this.students), { headers: headers }).subscribe();  
    32.         alert("Student Detail Inserted");  
    33.         this.getData();         
    34.     }  
    35.       
    36. }  

Next we need to add the boot.ts file to run our app.

Creating boot TypeScript file

Right Click on Scripts folder and click on add new Item. Select TypeScript File and name the file as boot.ts and click Add.

Add

In boot.ts file we add the below code. Here first we import bootstrap file to load and run our app file and also we import our app component. Note to import our app we need to give the same class name which was given in our app typescript file and give our app path in from as ’./app’ .Next we run our app by adding the app name in bootstrap as bootstrap(myAppComponent);.

  1. ///<reference path="../node_modules/angular2/typings/browser.d.ts" />    
  2.   
  3. import {bootstrap} from 'angular2/platform/browser'  
  4. import {ROUTER_PROVIDERS} from "angular2/router";  
  5. import {HTTP_PROVIDERS, HTTP_BINDINGS} from "angular2/http";  
  6. import {AppComponent} from './app'  
  7.   
  8. bootstrap(AppComponent, [HTTP_BINDINGS, HTTP_PROVIDERS]);  
Creating model TypeScript file

Right Click on Scripts folder and click on add new Item. Select TypeScript File and name the file as model.ts and click Add.

model

We use this model typescript file to create our Studentmasters model and declare all the public properties which we created in our MVC model.
  1. export class StudentMasters {  
  2.     constructor(  
  3.         public StdID: number,  
  4.         public StdName: string,  
  5.         public Email: string,  
  6.         public Phone: string,  
  7.         public Address: string  
  8.     ) { }  
  9. }  
Step 9: Creating HTML File

Next we need to create our html page to view result. To add the HTML file right click on wwwroot folder and click add new item, give the name as index.html and click Add.

HTML

In HTMl file replaces the below code. Here we can see first in header part we add all the script reference files and in script we load our boot file to run our app. In body part we display the result using the component selector name. In our app component we have given the selector name as “myapp1”. In this HTML in order to display the result we add tag like this <my-app>Please wait...</my-app>
  1. <html>  
  2.   
  3. <head>  
  4.     <title>ASP.NET Core: AngularJS 2 Demo</title>  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  6.     <!-- 1. Load libraries -->  
  7.     <!-- IE required polyfills -->  
  8.     <script src="/js/es6-shim.min.js"></script>  
  9.     <script src="/js/system-polyfills.js"></script>  
  10.     <!-- Angular2 libraries -->  
  11.     <script src="/js/angular2-polyfills.js"></script>  
  12.     <script src="/js/system.src.js"></script>  
  13.     <script src="/js/Rx.js"></script>  
  14.     <script src="/js/angular2.dev.js"></script>  
  15.     <script src="/js/router.dev.js"></script>  
  16.     <script src="/js/http.dev.js"></script>  
  17.     <!-- Bootstrap via SystemJS -->  
  18.     <script>  
  19.   
  20.         System.config  
  21.         ({  
  22.             packages:  
  23.             {  
  24.                 "app":  
  25.                 {  
  26.                     defaultExtension: 'js'  
  27.                 },  
  28.                 'lib': { defaultExtension: 'js' }  
  29.             }  
  30.         });  
  31.         System.import('app/boot').then(null, console.error.bind(console));  
  32.     </script>  
  33. </head>  
  34.   
  35. <body>  
  36.   
  37.     <table style='width: 99%;table-layout:fixed;'>  
  38.         <tr>  
  39.             <td>  
  40.                 <table style="background-color:#FFFFFF;border: dashed 3px #6D7B8D; padding:10px;width: 99%;table-layout:fixed;" cellpadding="12" cellspacing="12">  
  41.                     <tr style="height: 30px; background-color:#f06a0a ; color:#FFFFFF ;border: solid 1px #659EC7;">  
  42.                         <td align="center">  
  43.                             <h3> SHANU ASP.NET Core1.0 Insert/Select Student Details to Database Using Angular2 and WEB API </h3>  
  44.                         </td>  
  45.                     </tr>    
  46.   
  47.                 </table>  
  48.   
  49.             </td>  
  50.         </tr>  
  51.           
  52.         <tr>  
  53.             <td>  
  54.   
  55.   
  56.                 <!-- Application PlaceHolder -->  
  57.                 <my-app>Please wait...</my-app>  
  58.             </td>  
  59.         </tr>  
  60.         </table>  
  61.   
  62. </body>  
  63.   
  64. </html>   
So what is next --  we have successfully created our first Angular2 and Asp.Net core 1.0 web application and wait -- we have to do one more pending work to run our application. Yes we need to run our Grunt file to create our entire script file in our wwwroot scripts folder.

Step 10: Run the Grunt file using Visual Studio Task Runner Explorer

Now we need to run the Grunt file using Visual Studio Task Runner.

To view the Task Runner Click the menu View-> Other Windows,-> and click on Task Runner Explorer.

Explorer

We can also open Task Runner by right clicking on our Gruntfile.js in Solution Explorer and clicking on Task Runner Explorer.

Task Runner Explorer

Now we can see our Task Runner Explorer.

 Task Runner Explorer

Click on GruntFile.js and click on refresh button at top left.

GruntFile.js

Now we can see all the GruntFiles have been added.

GruntFile

Right click the default under Alias Task and click Run.

Run

Now our Grunt file has been successfully run in our project. When we add a script file we can see new app.js file will be created in our wwwroot folder.

folder

Run the Program

Here we can see all the data from WEB API has been bound to our HTML page using Angular2.You can insert new Student details In the same way you can add Update and Delete function to perform complete CRUD operations to maintain Student records.

Note: First create the Database and Table in your SQL Server. You can run the SQL Script from this article to create StudentsDB database and StudentMasters Table and also don’t forget to change the Connection string from “appsettings.json”.

output