Loading

ASP.NET Web API

How to Implement DELETE Method in Web API Application?. The Complete ASP.NET Web API Developer Course 2023 [Videos].

In this article, I am going to discuss how to Implement DELETE Method in Web API Applications with an example. Please read our previous article where we discussed how to Implement PUT Method Web API before proceeding to this article as we are going to work with the same example. As part of this article, we are going to discuss the following pointers.

  1. How to Implement the Delete Method in Web API Application?
  2. Testing Delete Method in Web API.
How to Implement the DELETE Method in ASP.NET Web API?

The Delete Method in Web API allows us to delete an item. We want to delete a specified employee from the Employees database table. To achieve this Include the following Delete method in EmployeesController.

public class EmployeesController : ApiController
{
public void Delete(int id)
{
using (EmployeeDBContext dbContext = new EmployeeDBContext())
{
dbContext.Employees.Remove(dbContext.Employees.FirstOrDefault(e => e.ID == id));
dbContext.SaveChanges();
}
}
}

At this point build the solution, run the application and fire up the Fiddler and issue a Delete request.

  1. Set the HTTP verb to DELETE
  2. Content-Type: application/json. This tells that we are sending JSON formatted data to the server
  3. Finally, click on the execute button as shown below

DELETE Method in WEB API

When we click on the Execute button, it will give us the below response

DELETE Method in WEB API

This works fine and deletes the employee record from the database as expected. The problem here is that since the return type of the Delete method is void, we get status code 204 No ContentWhen the Deletion is successful, we want to return status code 200 OK indicating that the deletion is successful.

Also when we try to delete an employee whose Id does not exist we get back HTTP status code 500 Internal Server ErrorWe get status code 500, because of a NULL reference exceptionIf an item is not found, then we need to return status code 404 Not Found.

How to Fix the above issues?

To fix both of these issues modify the code in the Delete method as shown below.

public class EmployeesController : ApiController
{
public HttpResponseMessage Delete(int id)
{
try
{
using (EmployeeDBContext dbContext = new EmployeeDBContext())
{
var entity = dbContext.Employees.FirstOrDefault(e => e.ID == id);
if (entity == null)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound,
"Employee with Id = " + id.ToString() + " not found to delete");
}
else
{
dbContext.Employees.Remove(entity);
dbContext.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK);
}
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
}

At this point, issue another DELETE request from the Fiddler. Notice in the response header we have status code 200 OKAlso, when we try to delete an employee whose id does not exist, we get the status code 404 Not Found instead of 500 Internal Server Error 

See All

Comments (317 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Web API / Blog

What is ASP.NET Web API Application?

In this ASP.NET Web API Tutorials series, I covered all the features of ASP.NET Web API. You will learn from basic to advance level features of ASP.NET Web API. The term API stands for “Application Programming Interface” and ASP.NET Web API is a framework provided by Microsoft which makes it easy to build Web APIs, i.e. it is used to develop HTTP-based web services on the top of .NET Framework.
3-Feb-2022 /34 /317

ASP.NET Web API / Blog

How to creat ASP.NET Web API Application using Visual Studio?

In this article, I am going to discuss the step-by-step procedure for Creating ASP.NET Web API Application. Please read our previous article before proceeding to this article where we gave an overview of the ASP.NET Web API framework. As part of this article, we ate going to discuss the following pointers.
3-Feb-2022 /34 /317

ASP.NET Web API / Blog

How to add Swagger in Web API Application?

In this article, I am going to discuss how to add Swagger in Web API Application to document and test restful Web API services. Please read our previous article where we discussed How to Create an ASP.NET Web API Application step by step before proceeding to this article as we are going to work with the same example. As part of this article, we are going to discuss the following pointers.
3-Feb-2022 /34 /317