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 use fiddler in this article. Hope you are familiar with the Web Proxy Tool fiddler.
Kindly visit this link to know more about Configuration over Convention in WebApi.
WebApi Configuration over Convention using MVC4 template
HTTP POST can support partial updates to a resource. But there is a separate PATCH method. This new HTTP method, PATCH, to modify 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 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 on Fiddler how it looks like in depict image.
Let’s create a sample application and achieve this step by step.
Step 1: Let’s first create a sample web application and using ASP.NET MVC 4 Web Application and named It with your choice as I gave WebApiDemo shown in depict image below:
Step2: Click ok and choose Web API option from the templates shown in wizard window.
Step3: 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 give a name of EmployeesController for the controller. Leave the option Empty API Controller selected in the Template dropdown and click Add, as shown in Figure below. Notice that the generated controller class inherits from ApiController, a class that is part of the ASP.NET Web API framework.Kinldy add the following code into 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.cs 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 as UpdateDetailsViaPatch, which follows the convention based (starts with Get, PUT, POST, Delete and PATCH) approach to call methods of WebApi with [FromBody]
as action parameter.
Note: FromBody contains whole request body of 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 below image:
Great WebApi is up and running.
This is the request which we’ve set into Fiddler as depict 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 debugger point. This is the convention based approach (Method Starts with Http Verb PATCH).
Press F5 again and see the result as shown below in image.
To verify that it has only updated the LastName of Dhnanjay.Paste this url (http://localhost:57888/api/employees/002) in browser and see the result as shown below in image.
Conclusion: In this article we looked into Patch update with help of FromBody.
Hope it will help you somewhere down the line J
Keep coding and Smile
Thanks
Sachin Kalia
0 comments :
Post a Comment