Ramco Ramco

Ramco Ramco

  • 441
  • 3.4k
  • 522.7k

Delete Api not working

Feb 25 2021 5:03 AM
Hi
 
I have below code. when i type in Url - http://localhost:40991/api/Employees/DeleteEmployee?id=1
 
It is going in public HttpResponseMessage GetEmployee(int id) . Why so
  1. public class EmployeesController : ApiController  
  2. {  
  3. private PMDbEntities db = new PMDbEntities();  
  4. // GET: api/Employees  
  5. public IHttpActionResult GetEmployees()  
  6. {  
  7. try  
  8. {  
  9. var results = (from d in db.Employees  
  10. join f in db.Departments  
  11. on d.DepartmentId equals f.ID  
  12. select new  
  13. {  
  14. Id = d.ID,  
  15. Name = d.Name,  
  16. Department = f.Description  
  17. }).ToList();  
  18. if (results == null)  
  19. {  
  20. return NotFound();  
  21. }  
  22. return Ok(results);  
  23. }  
  24. catch (Exception)  
  25. {  
  26. return BadRequest();  
  27. }  
  28. }  
  29. [ResponseType(typeof(Employee))]  
  30. public HttpResponseMessage GetEmployee(int id)  
  31. {  
  32. Employee employee = db.Employees.Find(id);  
  33. if (employee == null)  
  34. {  
  35. return Request.CreateErrorResponse(HttpStatusCode.NotFound,"Employee Code : " + id + "not found");  
  36. }  
  37. //return Request.CreateResponse(HttpStatusCode.OK,employee);  
  38. return Request.CreateResponse(HttpStatusCode.OK);  
  39. }  
  40. // DELETE: api/Employees/5  
  41. // [ResponseType(typeof(Employee))]  
  42. public IHttpActionResult DeleteEmployee(int id)  
  43. {  
  44. try  
  45. {  
  46. Employee employee = db.Employees.Find(id);  
  47. if (employee == null)  
  48. {  
  49. return Content(HttpStatusCode.NotFound, "Employee not found");  
  50. }  
  51. else  
  52. {  
  53. db.Employees.Remove(employee);  
  54. db.SaveChanges();  
  55. return Ok();  
  56. //(employee);  
  57. }  
  58. }  
  59. catch (Exception ex)  
  60. {  
  61. return BadRequest("Error Encountered : " + ex);  
  62. }  
  63. }  
  64. }  
Thanks

Answers (4)