WebApi: WebApi Patch Update Using FromBody Parameter in WebApi Using MVC4 Template

In this article, I’ll share my thoughts on Patch update using FromBody in WebApi Configuration over Convention. I’ve also used Fiddler in this article. I hope you are familiar with the Web Proxy Tool Fiddler.

Kindly visit this link to learn more about Configuration over Convention in the WebApi.

Configuration Over Convention in WebApi Using MVC4 Template

HTTP POST can support partial updates to a resource. But there is a separate PATCH method. This new HTTP method, PATCH, modifies an existing HTTP resource. The call to the Patch method is sufficient to partially update the corresponding properties of the Employee object in the list. For example, the request will update only the LastName property of the employee with the ID of 002 to “Thakur”, but in order to issue a PATCH request, you will need to use a tool like Fiddler.

Kindly have a look at Fiddler to understand how it looks like in the following image.

Fiddler

Let’s create a sample application and do this step-by-step.

Step 1: Let’s first create a sample web application and using ASP.NET MVC 4 Web Application and name it as you choose; I used WebApiDemo as shown in the following image:

create a sample web application

Step 2: Click OK and choose Web API from the templates shown in the wizard window.
 
Web API option

Step 3: You’ll find the application structure as shown below at first sight.
 
application structure

Step 4: Right-click the Controllers folder in the Solution Explorer of Visual Studio. Select "Add" ➤"Controller" and provide a name of EmployeesController for the controller. Leave the option Empty API Controller selected in the Template dropdown and click "Add", as shown in the figure below. Notice that the generated controller class inherits from ApiController, a class that is part of the ASP.NET Web API framework. Kindly add the following code into the EmployeesController class.
  1. public static IList<Employee> listEmp = new List<Employee>()  
  2. {  
  3.      new Employee()  
  4.      {  
  5.          ID =001, FirstName="Sachin", LastName="Kalia"  
  6.      },  
  7.      new Employee()  
  8.      {  
  9.          ID =002, FirstName="Dhnanjay" ,LastName="Kumar"  
  10.      },  
  11.      new Employee()  
  12.      {  
  13.          ID =003, FirstName="Ravish", LastName="Sindhwani"  
  14.      },  
  15.      new Employee()  
  16.      {  
  17.          ID =004, FirstName="Amit" ,LastName="Chaudhary"  
  18.      },  
  19. };
controller 

Step 5: Right-click the Models folder in the Solution Explorer of Visual Studio. Select "Add" -> "Class" to add a new class with a name of Employee.

add a new class

After creating the Employee class, kindly add the following code into this class.
  1. public class Employee  
  2. {  
  3.     public string ID { getset; }  
  4.     public string FirstName { getset; }  
  5.     public string LastName { getset; }  
  6. }   

Now I’ve created a method named UpdateDetailsViaPatch, that follows the convention based (starts with Get, PUT, POST, Delete and PATCH) approach to call methods of WebApi with [FromBody] as the action parameter.

HttpPATCH

Note: FromBody contains entire request body of the POST request, not a piece of the body, is bound to a parameter. For this reason, you cannot have multiple parameters with the FromBody attribute in an action method.

Press F5 and run your application; it will show the following image:
 
run your application

Great WebApi is up and running.

This is the request that we’ve set into Fiddler as depicted below:
 
request

Kindly paste this URL (http://localhost:57888/api/Employees/002 ) into Fiddler and click on Execute, it will reach into your code segment. Where you’ve set a debugging breakpoint. This is the convention based approach (the method starts with the HTTP Verb PATCH).
 
Method Starts with Http Verb PATCH

Press F5 again and see the result as shown below in the image.
 
result

To verify that it has only updated the LastName of Dhnanjay, paste this URL (http://localhost:57888/api/employees/002) in the browser and see the result as shown below in the image.
 
updated the LastName

Conclusion

In this article, we looked into the Patch update using FromBody.


Similar Articles