locked
Delete Api Issue RRS feed

  • Question

  • User1979860870 posted

    Hi

     When i give this Url - http://localhost:40991/api/Employees/DeleteEmployee?id=1 it is going in GetEmployee(int id)

        public class EmployeesController : ApiController
        {
            private PMDbEntities db = new PMDbEntities();
    
            // GET: api/Employees
            [HttpGet]
            public IHttpActionResult GetEmployees()
            {
                try
                {
                    var results = (from d in db.Employees
                                   join f in db.Departments
                                   on d.DepartmentId equals f.ID
                                   select new
                                   {
                                       Id = d.ID,
                                       Name = d.Name,
                                       Department = f.Description
                                   }).ToList();
                    if (results == null)
                    {
                        return NotFound();
                    }
                    return Ok(results);
                }
                catch (Exception)
                {
                    return BadRequest();
                }
            }
    
            [HttpGet]
            [ResponseType(typeof(Employee))]
            public HttpResponseMessage GetEmployee(int id)
            {
                Employee employee = db.Employees.Find(id);
                if (employee == null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound,"Employee Code : " + id + "not found");
                }
    
                //return Request.CreateResponse(HttpStatusCode.OK,employee);
                return Request.CreateResponse(HttpStatusCode.OK);
    
            }
    
             // DELETE: api/Employees/5
            [HttpDelete]
            [ResponseType(typeof(Employee))]
            public IHttpActionResult DeleteEmployee(int id)
            {
                try
                {
                    Employee employee = db.Employees.Find(id);
                    if (employee == null)
                    {
                        return Content(HttpStatusCode.NotFound, "Employee not found");
                    }
                    else
                    {
                        db.Employees.Remove(employee);
                        db.SaveChanges();
                        return Ok();
                        //(employee);
                    }
                }
                catch (Exception ex)
                {
                    return BadRequest("Error Encountered : " + ex);
                }
            }
    }

    Thanks

    Thursday, February 25, 2021 5:48 AM

All replies

  • User-474980206 posted

    Your delete action requires the verb delete, you appear to be passing get.

    note: the browsers does not support the delete verb, you must use Ajax.

    Thursday, February 25, 2021 6:55 AM
  • User1979860870 posted

    Hi Bruce

      Even in fiddler i am facing probe. It gives message Source not found.

    Secondly

    Get statement works ok - http://localhost:40991/api/Employees/

    When i write in PostMan with Get http://localhost:40991/api/Employees/1 this does not show any Data though Status is o.k

    Thanks

    Thursday, February 25, 2021 7:13 AM
  • User475983607 posted

    jagjit saini

    When i write in PostMan with Get http://localhost:40991/api/Employees/1 this does not show any Data though Status is o.k

    Your code is working exactly how you designed it to work.  You specify return Ok not the Employee record.  It's very odd that are able to return data from one action but not another.  

    Thursday, February 25, 2021 1:26 PM