Web Api is light weight Technology to build RESTfull Web Services.
Web Api is framework to build HTTP services that can be consumed by various clients.
To perform CRUD Operations, We will use some HTTP verbs, which is listed below.
- GET : It is used for Get data.
- POST : It is used to create a new resource.
- PUT : It is used to update the existing resource.
- DELETE : It is used to Delete the resources.
Now Let’s Create MVC application.
File => New => Project
Select Asp.Net MVC4 Application, Click OK.
A pop up window will open, Select Web Api from there.
 
 Add the following two class to model folder.
| 
01 
02 
03 
04 
05 
06 
07 
08 
09 
10 | namespaceStartWebApi.Models{    publicclassEmployee    {        publicintId { get; set; }        publicstringName { get; set; }        publicstringEmail { get; set; }        publicintAge { get; set; }    }} | 
| 
01 
02 
03 
04 
05 
06 
07 
08 
09 
10 | usingSystem.Data.Entity;namespaceStartWebApi.Models{    publicclassDemoContext : DbContext    {               publicDbSet<Employee> employee { get; set; }    }} | 
Now Create a Api Controller. Name it as
HomeApi
| 
01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 | usingStartWebApi.Models;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web.Http;usingSystem.Web.Http.Description;namespaceStartWebApi.Controllers{    publicclassHomeApiController : ApiController    {        publicIEnumerable<Employee> GetAll()        {            using(DemoContext objDemoContext = newDemoContext())            {                returnobjDemoContext.employee.ToList();            }        }        publicIHttpActionResult Get(intId)        {            using(DemoContext objDemoContext = newDemoContext())            {                varemployee = objDemoContext.employee.FirstOrDefault(a => a.Id == Id);                if(employee != null)                {                    returnOk(employee);                }                else                {                    returnNotFound();                }            }        }        [ResponseType(typeof(Employee))]        publicIHttpActionResult Post(Employee employee)        {            using(DemoContext objDemoContext=newDemoContext())            {                objDemoContext.employee.Add(employee);                objDemoContext.SaveChanges();            }            returnCreatedAtRoute("DefaultApi", new{ Id = employee.Id }, employee);        }        [ResponseType(typeof(Employee))]        publicIHttpActionResult Delete(intId)        {            using(DemoContext objDemoContext = newDemoContext())            {                varGetEmployee = objDemoContext.employee.FirstOrDefault(a => a.Id == Id);                if(GetEmployee != null)                {                    objDemoContext.employee.Remove(GetEmployee);                    objDemoContext.SaveChanges();                    returnOk();                }                else                {                    returnNotFound();                }            }        }        [ResponseType(typeof(Employee))]        publicIHttpActionResult Put(intId,Employee employee)        {            if(Id != employee.Id)            {                returnBadRequest();            }            else            {                using(DemoContext contextObj = newDemoContext())                {                    Employee GetEmployee = contextObj.employee.Find(Id);                    GetEmployee.Name = employee.Name;                    GetEmployee.Email = employee.Email;                    GetEmployee.Age = employee.Age;                    contextObj.SaveChanges();                    returnCreatedAtRoute("DefaultApi", new{ Id = employee.Id }, employee);                }            }        }    }} | 
Now create a Home Controller.
| 
01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 | usingSystem.Web.Mvc;namespaceStartWebApi.Controllers{    publicclassHomeController : Controller    {        publicActionResult Index()        {            returnView();        }    }} | 
Now Install AngularJs in your Project.
Write down following code in Index.cshtml.
| 
01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 | <divid="body">    <divng-controller="myCntrl"class="divList">        <spanng-click="AddEmployeeDiv()"class="btnGreen">            Add Employee        </span>        <divclass="divList">            <pclass="divHead">Employee List</p>            <tablecellpadding="12">                <tr>                    <td><b>ID</b></td>                    <td><b>Name</b></td>                    <td><b>Email</b></td>                    <td><b>Age</b></td>                    <td><b>Actions</b></td>                </tr>                <trng-repeat="employee in employees">                    <td>                        {{employee.Id}}                    </td>                    <td>                        {{employee.Name}}                    </td>                    <td>                        {{employee.Email}}                    </td>                    <td>                        {{employee.Age}}                    </td>                    <td>                        <spanng-click="editEmployee(employee)"class="btnGreen">Edit</span>                        <spanng-click="deleteEmployee(employee)"class="btnRed">Delete</span>                    </td>                </tr>            </table>        </div>        <divng-show="divEmployee">            <pclass="divHead">{{Action}} Employee</p>            <table>                <tr>                    <td><b>Id</b></td>                    <td>                        <inputtype="text"disabled="disabled"ng-model="employeeId"/>                    </td>                </tr>                <tr>                    <td><b>Name</b></td>                    <td>                        <inputtype="text"ng-model="employeeName"/>                    </td>                </tr>                <tr>                    <td><b>Email</b></td>                    <td>                        <inputtype="text"ng-model="employeeEmail"/>                    </td>                </tr>                <tr>                    <td><b>Age</b></td>                    <td>                        <inputtype="text"ng-model="employeeAge"/>                    </td>                </tr>                <tr>                    <tdcolspan="2">                        <inputtype="button"class="btnGreen"value="Save"ng-click="AddUpdateEmployee()"/>                    </td>                </tr>            </table>        </div>    </div></div> | 
Create a folder inside content folder. name it as Angular.
Add three Js file to it.
Module.js
| 
1 | varapp = angular.module("myApp",[]); | 
We will write code in service.Js file to Interact with Web Api to perform CRUD Operations.
| 
01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 | app.service("angularService", function($http) {    //get All Eployee    this.getEmployees = function() {        return$http.get("/api/HomeApi");    };    // get Employee By Id    this.getEmployee = function(employeeID) {        returnresponse = $http.get("/api/HomeApi/"+ employeeID);             }    // Update Employee     this.updateEmp = function(employeeID, employee) {        varresponse = $http({            method: "put",            url: "/api/HomeApi/"+employeeID,            data: JSON.stringify(employee),            dataType: "json"        });        returnresponse;    }    // Add Employee    this.AddEmp = function(employee) {        varresponse = $http({            method: "post",            url: "/api/HomeApi",            data: JSON.stringify(employee),            dataType: "json"        });        returnresponse;    }    //Delete Employee    this.DeleteEmp = function(employeeId) {        varresponse = $http({            method: "delete",            url: "/api/HomeApi/"+ employeeId        });        returnresponse;    }}); | 
- $http.get (url) : It will send HTTP get request.
- $http.post (url, data) : It will send HTTP post request.
- $http.put (url,data) : It will send HTTP put request.
- $http.delete (url) : It will send HTTP delete request.
Controller.js :
| 
01 
02 
03 
04 
05 
06 
07 
08 
09 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 | app.controller("myCntrl", function($scope, angularService) {    $scope.divEmployee = false;    GetAllEmployee();    //To Get All Records      functionGetAllEmployee() {        vargetData = angularService.getEmployees();        getData.then(function(emp) {            $scope.employees = emp.data;        },function() {            alert('Error in getting records');        });    }    $scope.editEmployee = function(employee) {        vargetData = angularService.getEmployee(employee.Id);        getData.then(function(emp) {            $scope.employee = emp.data;            $scope.employeeId = employee.Id;            $scope.employeeName = employee.Name;            $scope.employeeEmail = employee.Email;            $scope.employeeAge = employee.Age;            $scope.Action = "Update";            $scope.divEmployee = true;        }, function() {            alert('Error in getting record');        });    }    $scope.AddUpdateEmployee = function()    {        varEmployee = {            Name: $scope.employeeName,            Email: $scope.employeeEmail,            Age: $scope.employeeAge        };        vargetAction = $scope.Action;        if(getAction == "Update") {            Employee.Id = $scope.employeeId;            vargetData = angularService.updateEmp($scope.employeeId,Employee);            getData.then(function(msg) {                GetAllEmployee();                alert("Employee Id :- "+msg.data.Id+ " is Updated");                $scope.divEmployee = false;            }, function() {                alert('Error in updating record');            });        } else{            vargetData = angularService.AddEmp(Employee);            getData.then(function(msg) {                GetAllEmployee();                alert("Employee Name :- "+msg.data.Name+" is Added");                $scope.divEmployee = false;            }, function() {                alert('Error in adding record');            });        }    }    $scope.AddEmployeeDiv=function()    {        ClearFields();        $scope.Action = "Add";        $scope.divEmployee = true;    }    $scope.deleteEmployee = function(employee)    {        vargetData = angularService.DeleteEmp(employee.Id);        getData.then(function(msg) {            GetAllEmployee();            alert('Employee Deleted');        },function(){            alert('Error in Deleting Record');        });    }    functionClearFields() {        $scope.employeeId = "";        $scope.employeeName = "";        $scope.employeeEmail = "";        $scope.employeeAge = "";    }}); | 
| 
1 
2 
3 | <connectionStrings>    <addname="DemoContext"providerName="System.Data.SqlClient"connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-StartWebApi-20141228192300;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-StartWebApi-20141228192300.mdf"/>  </connectionStrings> | 
_Layout.cshtmlInclude AngularJs file inside head tag.
| 
1 
2 
3 
4 | <script src="~/Scripts/angular.js"></script> <script src="~/Content/Angular/Module.js"></script> <script src="~/Content/Angular/Service.js"></script> <script src="~/Content/Angular/Controller.js"></script> | 
See the Previous Article, To know more about AngularJs Directives
We have done the CRUD Operations in MVC using Web Api and AngularJS.
DEMO

DOWNLOAD CODE
 
