
Introduction
In this article we will see in detail how to create ASP.NET Core 1.0 MVC 6 using WEB API and AngularJS .
You can also view our previous article.
Prerequisites
Visual Studio 2015: You can download it from here.
ASP.NET 5 /Core 1.0: download ASP.NET 5 RC from this link.
In this article we will see in detail how to:
- Create our ASP.NET Core 1.0 Web Application.
- How to change the Default Connection string to our SQL Server Connection String.
- How to add Model and DbContext Class for using in WEB API
- How to add Entity framework Service in Startup.cs file.
- How to add our WEB API Controller.
- How to add grunt package using NPM configuration file
- How to configure Grunt File.
- How to Run the Grunt file using Visual Studio Task Runner Explorer
- How to create our AngularJS Module, Controller and Service file and get WEB API data for bind in MVC page.
Reference link
Using the code
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.
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.
- USEMASTER
- GO
- -- 1) Check for the Database Exists .If the database is exist then drop and create new DB
- IFEXISTS(SELECT [name] FROMsys.databasesWHERE [name] ='StudentsDB')
- DROPDATABASEStudentsDB
- GO
- CREATEDATABASEStudentsDB
- GO
- USEStudentsDB
- GO
- -- 1) //////////// StudentMasters
- IFEXISTS(SELECT [name] FROMsys.tablesWHERE [name] ='StudentMasters')
- DROPTABLEStudentMasters
- GO
- CREATETABLE [dbo].[StudentMasters](
- [StdID] INTIDENTITYPRIMARYKEY,
- [StdName] [varchar](100)NOTNULL,
- [Email] [varchar](100)NOTNULL,
- [Phone] [varchar](20)NOTNULL,
- [Address] [varchar](200)NOTNULL
- )
- -- insert sample data to Student Master table
- INSERTINTO [StudentMasters]([StdName],[Email],[Phone],[Address])
- VALUES ('Shanu','[email protected]','01030550007','Madurai,India')
- INSERTINTO [StudentMasters]([StdName],[Email],[Phone],[Address])
- VALUES ('Afraz','[email protected]','01030550006','Madurai,India')
- INSERTINTO [StudentMasters]([StdName],[Email],[Phone],[Address])
- VALUES ('Afreen','[email protected]','01030550005','Madurai,India')
- select*from [StudentMasters]
After installing both Visual Studio 2015 and ASP.NET 5 RC.click Start, then Programs and select Visual Studio 2015 - Click Visual Studio 2015. Click New, then Project, select Web and select ASP.NET Web Application. Enter your Project Name and click OK.

Select Web Application under ASP.NET 5 Template and click ok.

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 5 we need to use “appsettings.json” file instead of web.config. Yes in ASP.NET 5 there is no web.Config file for connection string we need use the “appsettings.json” .We can find the “appsettings.json” file from our Asp.NET 5 solution.

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

Now the default connectionstring will be something like this,
- "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=aspnet5-MYASP.NET5DemoTest-afb3aac0-d181-4278-8436-cafeeb5a8dbf;Trusted_Connection=True;MultipleActiveResultSets=true"
- "ConnectionString": "Server=YourSQLSERVERNAME;Database=StudentsDB;user id=SQLID;password=SQLPWD;Trusted_Connection=True;MultipleActiveResultSets=true;"

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

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

Here our class will be look like the below image. Here we will add our model field property.

Add the header file usingSystem.ComponentModel.DataAnnotations; and add all our table field name as property in this model class like the below code.
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Threading.Tasks;
- usingSystem.ComponentModel.DataAnnotations;
- namespace MYASP.NET5DemoTest.Models
- {
- publicclassStudentMasters
- {
- [Key]
- publicintStdID
- {
- get;
- set;
- }
- [Required]
- [Display(Name = "Name")]
- publicstringStdName
- {
- get;
- set;
- }
- [Required]
- [Display(Name = "Email")]
- publicstring Email
- {
- get;
- set;
- }
- [Required]
- [Display(Name = "Phone")]
- publicstring Phone
- {
- get;
- set;
- }
- publicstring Address
- {
- get;
- set;
- }
- }
- }
Creating DbContext
Now we need to create a DBContext for our Entity Framework. Same as with Model Class, add a new class to our Models folder.
Right click the Models folder and click add new Item .Select Class and enter your class name as “StudentMastersAppContext.cs”

Here our class will be look like the below image.

Now first we need to add the header file for Entity framework as usingMicrosoft.Data.Entity;
Next inherit the DbContextto our class and then create object for our DBContext like below code.
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Threading.Tasks;
- usingMicrosoft.Data.Entity;
- namespace MYASP.NET5DemoTest.Models
- {
- publicclassStudentMastersAppContext: DbContext
- {
- publicDbSet < StudentMasters > Students
- {
- get;
- set;
- }
- }
- }
Now we can created our DB context and the next step is to add a Service for our Entity Framework.
Adding Entity Framework Service in Startup.cs
Next we need to add our Entity Framework service in Startup.cs . We can find the Startup.cs file from our solution explorer .

Open the Startup.csfile and we can see by default the ApplicationDBContext will be added in ConfigureServices method.

Now we can add one more DBContext for our StudentMastersAppContext like below code.
- // Add Entity Framework
- services.AddEntityFramework()
- .AddSqlServer()
- .AddDbContext < StudentMastersAppContext > (options =>
- options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
- In ConfigureServices method we add like this code below.
- publicvoidConfigureServices(IServiceCollection services)
- {
- // Add framework services.
- services.AddEntityFramework()
- .AddSqlServer()
- .AddDbContext < ApplicationDbContext > (options =>
- options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
- services.AddIdentity < ApplicationUser, IdentityRole > ()
- .AddEntityFrameworkStores < ApplicationDbContext > ()
- .AddDefaultTokenProviders();
- services.AddMvc();
- // Add Entity Framework
- services.AddEntityFramework()
- .AddSqlServer()
- .AddDbContext < StudentMastersAppContext > (options =>
- options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
- // Add application services.
- services.AddTransient < IEmailSender, AuthMessageSender > ();
- services.AddTransient < ISmsSender, AuthMessageSender > ();
- }
Creating our WEB API Controller
Right click the Controller folder and click add new Item .Select API Controller with Action, using Entity Framework and click Add.

Now we need to select our newly created Model class and our Data Context Class.
Model Class: In Model Class select our Model Class which we created as “StudentMasters”.
Data Context Class: In Data Context select our DBContext class which we created as “StudentMastersAppContext”

Give our Controller name with API and click ADD.
Now we can see our WEB API Controller has been created,

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/StudentMasters/
Run the program and paste the above API path to test our output.

Create Scripts and Controller Folder:
Next we need to add a new folder in our project for AngularJs.
Create a new folder named “Scripts” .Right click our project and click add new Folder and name the folder as “Scripts”

Now we create one more folder for our AngularJs controller named as “Controllers”

Adding 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 we can see NPM file as “package.JSON”

Dependencies Folder:

In dependency folder we can see the entire installed NPM package. Here we can see by default grunt package has not been installed .Here we will see how to add the grunt package.
If this “package.JSON” is not available
Right click our Project and click Add New Item to add our NPM Configuration File. Select Client-Side and Select NPM Configuration File and click Add.

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.

Here we have changed the name as our Solution name and also added the grunt package.
- {
- "name": "testforDemo",
- "version": "0.0.0",
- "devDependencies":
- {
- "gulp": "3.8.11",
- "gulp-concat": "2.5.2",
- "gulp-cssmin": "0.1.7",
- "gulp-uglify": "1.2.0",
- "rimraf": "2.2.8",
- "grunt": "0.4.5",
- "grunt-contrib-uglify": "0.9.1",
- "grunt-contrib-watch": "0.6.1"
- }
- }



















Ajith RamawickramaPosted Jun 9, 2016, 2:02 PM
Nice article Syed. Thank you. I have once question. Dont we have to add angularjs files to this project?
Kumaresh RajalingamPosted Mar 9, 2016, 8:39 PM
Nice share
Sridhar SharmaPosted Mar 9, 2016, 2:56 PM
Nice Share
Mohammed IbrahimPosted Mar 9, 2016, 9:24 AM
nice
RubolPosted Mar 9, 2016, 1:20 AM
Nice
Shailesh UkePosted Mar 9, 2016, 1:03 AM
Nice Article..
Debasis SahaPosted Mar 9, 2016, 12:08 AM
Nice one...
Saillesh PawarPosted Mar 8, 2016, 11:08 PM
Great article Syed sir
Humayun Kabir MamunPosted Mar 8, 2016, 10:57 PM
Nice...
Ankur MistryPosted Mar 8, 2016, 3:13 PM
Super Syed Shanu