vendredi 31 janvier 2020

Delete method is not working in web api core

Iam using web api core in my application get and post method is working fine

these are code

 public class DepartmentController : Controller
    {
        [HttpGet]
       [Route("api/Departments")]
        public List<Department> Get()
        {
            DepartmentDbContext Db = new DepartmentDbContext();
            List<Department> Departments = Db.departments.ToList();
            return Departments;

        }
        [HttpPost]
        [Route("api/Departments")]      
        public string Post([FromBody] Department department)
        {
            DepartmentDbContext Db = new DepartmentDbContext();      
                try
                {
                    Db.departments.Add(department);
                    Db.SaveChanges();
                    // return department;
                    return "Added Successfully";
                }
                catch (Exception)
                {
                    return "Record Not Added";
                }    
        }
}

the above code working fine and also worked fine in postman also

but delete method is not working

here is the code

[HttpDelete]
[Route("api/Departments")]
public int Delete(int id)
{
    DepartmentDbContext Db = new DepartmentDbContext();
    try
    {
        Department dept = Db.departments.Where(x => x.DepartmentID == id).FirstOrDefault();
        Db.departments.Remove(dept);
        Db.SaveChanges();
        return 1; 
    }
    catch(Exception)
    {

        return -1; 
    }
}

this code is not working in postman also

how to solve this

Regards

Baiju




Aucun commentaire:

Enregistrer un commentaire