Ramco Ramco

Ramco Ramco

  • 471
  • 2.8k
  • 393.4k

httpStatusCode:not found when trying to Delete

Feb 24 2021 7:13 AM
Hi
 
Trying to delete record but it is giving above error. Get is working o.k
  1. public class EmployeesController: ApiController {  
  2.   private PMDbEntities db = new PMDbEntities(); 
  3. // GET: api/Employees   
  4.   public IHttpActionResult GetEmployees() {  
  5.     try {  
  6.       var results = (from d in db.Employees join f in db.Departments on d.DepartmentId equals f.ID select new {  
  7.         Id = d.ID, Name = d.Name, Department = f.Description  
  8.       }).ToList();  
  9.       if (results == null) {  
  10.         return NotFound();  
  11.       }  
  12.       return Ok(results);  
  13.     } catch (Exception) {  
  14.       return BadRequest();  
  15.     }  
  16.   } // DELETE: api/Employees/5   
  17.   [ResponseType(typeof (Employee))] public IHttpActionResult DeleteEmployee(int id) {  
  18.     try {  
  19.       Employee employee = db.Employees.Find(id);  
  20.       if (employee == null) {  
  21.         return Content(HttpStatusCode.NotFound, "Employee not found");  
  22.       } else {  
  23.         db.Employees.Remove(employee);  
  24.         db.SaveChanges();  
  25.         return Ok(employee);  
  26.       }  
  27.     } catch (Exception ex) {  
  28.       return BadRequest("Error Encountered : " + ex);  
  29.     }  
  30.   }  
  31. }  
  1. protected void btnDelete_Click(object sender, EventArgs e) {  
  2.   RestClient restClient = new RestClient("http://localhost:40991/api/Employees/");  
  3.   restClient.Timeout = -1;  
  4.   RestRequest request = new RestRequest(Method.DELETE);  
  5.   request.AddHeader("Id", lblId.Text);  
  6.   request.AddHeader("Content-Type""application/json");  
  7.   IRestResponse result = restClient.Execute(request);  
  8. }  
Secondly, I want to display return value from Api
 
Thanks

Answers (4)