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 -
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.
Now, open fiddler and select PUT Verb. Specify the ID and the JSON text.
Here, we are sending JSON formatted data, by using Content-Type Header. Let’s execute the request.
When executed, let's see if the record is updated or not.
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.
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.
Here, I am updating an employee 10 which does not exist. Execute that request and you will see the following screen.
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.
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 -
- public HttpResponseMessage put(int id, [FromBody] employeesData employee) {
- using(TESTEntities entities = new TESTEntities()) {
- var entity = entities.employeesDatas.FirstOrDefault(e => e.ID == id);
- if (entity == null) {
- return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id = " + id.ToString() + "not found");
- } else {
- entity.FirstName = employee.FirstName;
- entity.LastName = employee.LastName;
- entity.Gender = employee.Gender;
- entity.Salary = employee.Salary;
- entities.SaveChanges();
- return Request.CreateResponse(HttpStatusCode.OK, entity);
- }
- }
- }
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.
We get "HTTP 404 not found" when you see JSON tab we will see appropriate message.
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.
We will see,
We got HTTP 200 Ok status code now click on JSON tab we will see the data,
The corresponding JSON data got updated.
In SQL server the data got updated.
Conclusion - So, this was all about implementing PUT method in ASP.NET Web API . Hope this article was helpful.