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.
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:
Step 2: Click OK and choose Web API from the templates shown in the wizard window.
Step 3: You’ll find the application structure as shown below at first sight.
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.
- public static IList<Employee> listEmp = new List<Employee>()
- {
- new Employee()
- {
- ID =001, FirstName="Sachin", LastName="Kalia"
- },
- new Employee()
- {
- ID =002, FirstName="Dhnanjay" ,LastName="Kumar"
- },
- new Employee()
- {
- ID =003, FirstName="Ravish", LastName="Sindhwani"
- },
- new Employee()
- {
- ID =004, FirstName="Amit" ,LastName="Chaudhary"
- },
- };
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.
After creating the Employee class, kindly add the following code into this class.
- public class Employee
- {
- public string ID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- }
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.
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:
Great WebApi is up and running.
This is the request that we’ve set into Fiddler as depicted below:
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).
Press F5 again and see the result as shown below in the image.
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.
Conclusion
In this article, we looked into the Patch update using FromBody.