collar.white
collar.white

Reputation: 13

call to DELETE api does not work on Postman

GET / POST / PUT, api calls work on Postman.

The vanilla Delete or custom delete do not.

a. regular DELETE

URL call in Postman - http://localhost:59510/api/Employee/123

ERROR in Postman -

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:59510/api/Employee/123'.",
    "MessageDetail": "No action was found on the controller 'Employee' that matches the request."
}

Code:

[HttpDelete]
public string Delete(int empID)
{
    try
    {
        string sSQL = $@"DELETE dbo.Employee WHERE emp_id='J-L12345M'";

        DataTable dt = new DataTable();

        using (var connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["WebAPIConn"].ConnectionString))
        {
            using (var cmd = new SqlCommand(sSQL, connStr))
            {
                using (var da = new SqlDataAdapter(cmd))
                {
                    cmd.CommandType = CommandType.Text;
                    da.Fill(dt);
                }

            }
        }

        return "Deleted Successfully From EMPLOYEE table!!";
    }
    catch
    {
        return "Failed to Delete From EMPLOYEE table";
    }
}

b. Custom Delete

URL call in Postman - http://localhost:59510/api/Employee/DeleteEmployee/123

Error In Postman - 404 error

[Route("api/Employee/DeleteEmployee")]
[HttpDelete]
public string DeleteEmployee(int empID)
{
    try
    {
        string sSQL = $@"DELETE dbo.Employee WHERE emp_id='{empID}'";

        DataTable dt = new DataTable();

        using (var connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["WebAPIConn"].ConnectionString))
        {
            using (var cmd = new SqlCommand(sSQL, connStr))
            {
                using (var da = new SqlDataAdapter(cmd))
                {
                    cmd.CommandType = CommandType.Text;
                    da.Fill(dt);
                }

            }
        }

        return "Deleted Successfully From EMPLOYEE table!!";
    }
    catch
    {
        return "Failed to Delete From EMPLOYEE table";
    }
}

Upvotes: 1

Views: 2109

Answers (1)

Krishna Varma
Krishna Varma

Reputation: 4260

try adding empID to route

[Route("api/Employee/DeleteEmployee/{empID}")]
[HttpDelete]
public string DeleteEmployee(int empID)

Upvotes: 2

Related Questions