Introduction

This article walks you through the steps to create a Web Application, using AngularJS, which uses WebAPI to communicate between client and Server side.

Step 1

Create ASP.NET Web API Application

Check the link given below to see all the steps to create a Web API wtih an Entity Framework code first implementation.

http://social.technet.microsoft.com/wiki/contents/articles/26795.asp-net-webapi-entity-framework-code-first.aspx

Step 2

Install NuGet

Now, in order to use an Entity Framework, we need to install a NuGet package.

In Visual Studio 2013, select the menu option given below.

Tools-> Library Package manager -> Manage NuGet Packages for the solution.

Search for AngularJS and select the option Install.

AngularJs

This option will automatically install the NuGet package.

Step 3

Create JavaScript Controller

Now, add a new JavaScript file (contactController.js) in scripts directory and add Angular functions.

JavaScript

  1. function contactController($scope, $http) {
  2. $scope.loading = true;
  3. $scope.addMode = false;
  4. //Used to display the data
  5. $http.get('/api/Contact/').success(function (data) {
  6. $scope.Contacts = data;
  7. $scope.loading = false;
  8. })
  9. .error(function () {
  10. $scope.error = "An Error has occured while loading posts!";
  11. $scope.loading = false;
  12. });
  13. $scope.toggleEdit = function () {
  14. this.Contact.editMode = !this.Contact.editMode;
  15. };
  16. $scope.toggleAdd = function () {
  17. $scope.addMode = !$scope.addMode;
  18. };
  19. //Used to save a record after edit
  20. $scope.save = function () {
  21. alert("Edit");
  22. $scope.loading = true;
  23. var frien = this.Contact;
  24. alert(emp);
  25. $http.put('/api/Contact/', frien).success(function (data) {
  26. alert("Saved Successfully!!");
  27. emp.editMode = false;
  28. $scope.loading = false;
  29. }).error(function (data) {
  30. $scope.error = "An Error has occured while Saving Contact! " + data;
  31. $scope.loading = false;
  32. });
  33. };
  34. //Used to add a new record
  35. $scope.add = function () {
  36. $scope.loading = true;
  37. $http.post('/api/Contact/', this.newContact).success(function (data) {
  38. alert("Added Successfully!!");
  39. $scope.addMode = false;
  40. $scope.Contacts.push(data);
  41. $scope.loading = false;
  42. }).error(function (data) {
  43. $scope.error = "An Error has occured while Adding Contact! " + data;
  44. $scope.loading = false;
  45. });
  46. };
  47. //Used to edit a record
  48. $scope.deleteContact = function () {
  49. $scope.loading = true;
  50. var Contactid = this.Contact.ContactId;
  51. $http.delete('/api/Contact/' + Contactid).success(function (data) {
  52. alert("Deleted Successfully!!");
  53. $.each($scope.Contacts, function (i) {
  54. if ($scope.Contacts[i].ContactId === Contactid) {
  55. $scope.Contacts.splice(i, 1);
  56. return false;
  57. }
  58. });
  59. $scope.loading = false;
  60. }).error(function (data) {
  61. $scope.error = "An Error has occured while Saving Contact! " + data;
  62. $scope.loading = false;
  63. });
  64. };
  65. }

Step 4

Create View

On BundlesConfig file, add the lines of code given below.

C#

  1. bundles.Add(new ScriptBundle("~/bundles/angularjs").Include(
  2. "~/Scripts/angular.js",
  3. "~/Scripts/contactController.js"));

Open _Layout.cshtml page from shared folder and add these two lines to render AngularJS and contactController in an Application.

JavaScript

  1. @Scripts.Render("~/bundles/angularjs")

Now, let’s work on View.

HTML

  1. @{
  2. Layout = "~/Views/Shared/_Layout.cshtml";
  3. }
  4. <h2>Contacts</h2>
  5. <div ng-app="" ng-controller="contactController" class="container">
  6. <strong class="error">{{ error }}</strong>
  7. <div class="row">
  8. <div class="logs-table col-xs-12">
  9. <table class="table table-bordered table-hover" style="width:100%">
  10. <tr>
  11. <th>Id</th>
  12. <th>Name</th>
  13. <th>Address</th>
  14. <th>City</th>
  15. <th>Country</th>
  16. </tr>
  17. <tr ng-repeat="contact in contacts">
  18. <td>{{ contact.Id }}</td>
  19. <td>{{ contact.Name }}</td>
  20. <td>{{ contact.Address }}</td>
  21. <td>{{ contact.City }}</td>
  22. <td>{{ contact.Country }}</td>
  23. </tr>
  24. </table>
  25. </div>
  26. </div>
  27. </div>

Step 5

Run an Application

Run the Application to see the output.

output
Resources

Some good resources about Windows Azure can be found at