Step 1: Open visual studio-2012 select MVC template and select WEB API tab.
Step 2: Create database.
Step 3: Create Model class through Entity Framework and Build that class.
Step 4: Generate bydefault HomeController and ValueController.
ValueController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. using WebAPIExample.Models;
  8. using System.Data.Entity;
  9. namespace WebAPIExample.Controllers
  10. {
  11. public class ValuesController: ApiController
  12. {
  13. BMGEntities1 db = new BMGEntities1();
  14. // GET api/values
  15. public IEnumerable < string > Get()
  16. {
  17. return new string[] {
  18. "value1", "value2"
  19. };
  20. }
  21. // GET api/values/5
  22. [HttpGet]
  23. public EmpRecord GetEmp(int id)
  24. {
  25. EmpRecord obj = db.EmpRecords.Find(id);
  26. return obj;
  27. }
  28. // POST api/values
  29. [HttpPost]
  30. public string AddEmp(EmpRecord obj)
  31. {
  32. db.EmpRecords.Add(obj);
  33. db.SaveChanges();
  34. return "record inserted successfully....";
  35. }
  36. // PUT api/values/5
  37. [HttpPut]
  38. public string EditEmp(EmpRecord obj)
  39. {
  40. EmpRecord obj2 = db.EmpRecords.SingleOrDefault(e1 = > e1.EId == obj.EId);
  41. obj2.Ename = obj.Ename;
  42. obj2.Sal = obj.Sal;
  43. db.SaveChanges();
  44. return "record updated successfully.......";
  45. }
  46. // DELETE api/values/5
  47. [HttpDelete]
  48. public string DeleteEmp(int id)
  49. {
  50. EmpRecord obj = db.EmpRecords.Find(id);
  51. db.EmpRecords.Remove(obj);
  52. db.SaveChanges();
  53. return "record deleted successfully......";
  54. }
  55. }
  56. }
HomeController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace WebAPIExample.Controllers
  7. {
  8. public class HomeController: Controller
  9. {
  10. public ActionResult Index()
  11. {
  12. return View();
  13. }
  14. }
  15. }
Step 5: Create view for index actionmethod.
Index.cshtml
  1. @{
  2. ViewBag.Title = "Index";
  3. }
  4. <h2>Web API</h2>
  5. <script src="~/Scripts/jquery-1.10.2.min.js"></script>
  6. <script>
  7. function fn_GetEmp() {
  8. var eno = $("#Eid").val();
  9. $.ajax({
  10. url: "http://localhost:1533/api/Values/" + eno,
  11. type: "GET",
  12. contentType: "application/json; charset=utf-8",
  13. dataType: "JSON",
  14. success: function (response) {
  15. $("#Ename").val(response.Ename);
  16. $("#Sal").val(response.Sal);
  17. }
  18. });
  19. }
  20. function fn_AddEmp() {
  21. var empData =
  22. {
  23. "Eid": $("#Eid").val(),
  24. "Ename": $("#Ename").val(),
  25. "Sal": $("#Sal").val(),
  26. };
  27. $.ajax({
  28. data:JSON.stringify(empData),
  29. url: "http://localhost:1533/api/Values",
  30. type: "POST",
  31. contentType: "application/json; charset=utf-8",
  32. dataType: "JSON",
  33. success: function (response) {
  34. $("#sp1").text("Inserted suceessfully....");
  35. }
  36. });
  37. }
  38. function fn_EditEmp() {
  39. var empData =
  40. {
  41. "Eid": $("#Eid").val(),
  42. "Ename": $("#Ename").val(),
  43. "Sal": $("#Sal").val(),
  44. };
  45. Eno= $("#Eid").val(),
  46. $.ajax({
  47. data: JSON.stringify(empData),
  48. url: "http://localhost:1533/api/Values/"+Eno,
  49. type: "PUT",
  50. contentType: "application/json; charset=utf-8",
  51. dataType: "JSON",
  52. success: function (response) {
  53. $("#sp1").text("Updated successfully....");
  54. }
  55. });
  56. }
  57. function fn_DeleteEmp() {
  58. var Eid = $("#Eid").val();
  59. $.ajax({
  60. url: "http://localhost:1533/api/Values/" + Eid,
  61. type: "Delete",
  62. contentType: "application/json; charset=utf-8",
  63. dataType: "JSON",
  64. success: function (response) {
  65. $("#sp1").text("Deleted successfully....");
  66. }
  67. });
  68. }
  1. undefined</script>
  2. @Html.Label("Eid")
  3. @Html.TextBox("Eid")
  4. undefined<br />undefined<br />
  5. @Html.Label("EName")
  6. @Html.TextBox("Ename")
  7. undefined<br />undefined<br />
  8. @Html.Label("Sal")
  9. @Html.TextBox("Sal")
  10. undefined
  11. <br />undefined
  12. <br />undefined
  13. <input type="button" value="GetEmp" onclick="fn_GetEmp()" />undefined
  14. <input type="button" value="AddEmp" onclick="fn_AddEmp()"/>undefined
  15. <input type="button" value="EditEmp" onclick="fn_EditEmp()" />undefined
  16. <input type="button" value="DeleteEmp" onclick="fn_DeleteEmp()" />undefined
  17. <hr />undefined<span id="sp1">
  18. </span>
Step 6: Build and Run the project.