HttpPut Method In ASP.NET Web API - Part Five

In the previous article of the ASP.NET Web API series, I  used HttpPost method of HTTP, by which we can insert a member into our specific database. Hence, to insert the information as per our expectations, we use HttpPost method by using a URL. In this series of articles, I’ll work on Put() method of HTTP. You can follow my ASP.NET Web API series for the purposes of how the Get() and Post() method requests work in the given following links –

Now, we’ll work on another method of HTTP, which is Put() method, which is responsible for updating data to the Server. I’m going to work with my previous project Entity Framework, that I have created in a previous series of articles Using Entity Framework In ASP.NET Web API - Part Three.

In this, I will show you how we can update the data, which will be saved in MySQL database by using the Put() method. Hence, just flip to Visual Studio and open your previous project.

Under the IEmployeTest Interface, create a method to update a record of the employee, followed by the table name, i.e., Employee.
  1. string updateEmploye(Employe mode);  
Here is the screenshot of IEmployeInterface -
 
 

In the EmployeTest class, the object of EmployeEntites1, which is our context, returns the existing table record in the database. Pass the modified entity into the Entry method to get its DBEntityEntry object and then mark its state as Modified. Call SaveChanges() method to update the employe information into the database.
  1. public string updateEmploye(Employe mode) {  
  2.     EmployeeEntities1 EM = new EmployeeEntities1();  
  3.     Employe emps = db.Employes.Where(c => c.EmpID == mode.EmpID).Single < Employe > ();  
  4.     emps.EmpID = mode.EmpID;  
  5.     emps.EmpMoNo = mode.EmpMoNo;  
  6.     emps.EmpAddress = mode.EmpAddress;  
  7.     emps.EmpName = mode.EmpName;  
  8.     emps.EmpProfession = mode.EmpProfession;  
  9.     EM.Entry(emps).State = System.Data.Entity.EntityState.Modified;  
  10.     EM.SaveChanges();  
  11.     return "Hey!! Amit your Employee Data Updated Successfully...";  
  12. }  
Follow the screenshot of EmployeTest class.
 


We need to make some changes in the Controller as well. I have to implement the updateEmploye() method here, using the [HttpPut] method.
  1. [HttpPut]  
  2. public string UpdateEmploye(Employe mode) {  
  3.     return empTest.updateEmploye(mode);  
  4.   
  5. }  
Follow the screenshot of Employee controller -
 


Now, all the code is set into the Visual Studio. Flip to your SQL Server Management. I have two records in the Employee table. Let’s suppose I want to update the second row that holds EmpID 2.
 


Similarly to previous articles, I’m using here Postman for the PUT request. Open Postman and call the PUT method. Enter your Web API URL and make sure your Visual Studio project is in running mode. For this request, I need to call updateEmploye method in the URL. Under the body, you have to write the JSON script of your data which you want to update. Finally, click the send button to make a request.
 


Look at the screenshot, I have a successful message. It means our data has been updated in the database.
 


Thus, I’ve successfully updated my second record.
 

 
Thanks for reading this article. Stay tuned with us for more on ASP.NET Web API.