Implementing PUT Method In ASP.NET Web API

Overview

In this article, you will see how to implement PUT method in ASP.NET Web API.

Introduction

Let’s flip to Visual Studio now. In our employee Controller, let us implement the PUT method. This method has two input parameters - (i) the id of the employee who we want to update, and (ii) the employee object with which we want to update. We also have to tell the employee object where the data is coming from. So, we implement the PUT method as -

ASP.NET

Here, we are using fromBody function to define the instance from where the data is coming. We are using TESTentites instance.
Now, we have taken a variable called "entity" in which we are updating the first or default value retrieved by our lambda value. Then, define the respective fields that we want to update. Finally, save the changes. So now, build you app and run your solution. Then, open fiddler, in order to monitor this request here. Before that, we will update the id with 5, as shown in the below screenshot.

ASP.NET

Now, open fiddler and select PUT Verb. Specify the ID and the JSON text.

ASP.NET

Here, we are sending JSON formatted data, by using Content-Type Header. Let’s execute the request.

ASP.NET

When executed, let's see if the record is updated or not.

ASP.NET

As you can see, ID with 5 has been updated successfully.

Now, the code is working as expected. But, there are some problems in the code. First the return type is Void. Look at the status code that we are getting back.

ASP.NET

We are getting "204 : No content". When an update is successful, we should be returning the status code "200 OK" which indicates to the user that update is successful. The second problem is that when we try to update an employee whose ID does not exist, we get "500 Internal Server Error".
 
Let’s look that in action.

ASP.NET

Here, I am updating an employee 10 which does not exist. Execute that request and you will see the following screen.

ASP.NET

We are getting 500 Internal Server Error. We are getting this status code because of an exception. If you want to know the type of exception that has occurred, click on the JSON tab.

JSON

We are getting here NULLReferenceException . So, we will write a code that will handle the NULL values.

The first thing we will try to do here is to change the return type to httpResponseMessage and check if the entity is NULL. Once the update is successful, we will return 200 OK. So, our code is -
  1. public HttpResponseMessage put(int id, [FromBody] employeesData employee) {  
  2.     using(TESTEntities entities = new TESTEntities()) {  
  3.         var entity = entities.employeesDatas.FirstOrDefault(e => e.ID == id);  
  4.         if (entity == null) {  
  5.             return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id = " + id.ToString() + "not found");  
  6.         } else {  
  7.             entity.FirstName = employee.FirstName;  
  8.             entity.LastName = employee.LastName;  
  9.             entity.Gender = employee.Gender;  
  10.             entity.Salary = employee.Salary;  
  11.             entities.SaveChanges();  
  12.             return Request.CreateResponse(HttpStatusCode.OK, entity);  
  13.         }  
  14.     }  
  15. }  
Now, build you app and we will monitor the request in fiddler. First, we will try to update the employee whose ID does not exist.



Execute the request.

Execute

We get "HTTP 404 not found" when you see JSON tab we will see appropriate message.

Execute

We had got the desired message here as the ID with 10 is not found. Now let’s update employee whose ID is 1 and execute that request.

Execute

We will see,

Execute

We got HTTP 200 Ok status code now click on JSON tab we will see the data,

Execute

The corresponding JSON data got updated.

In SQL server the data got updated.

Execute

Conclusion - So, this was all about implementing PUT method in ASP.NET Web API . Hope this article was helpful.


Similar Articles