Create PUT Method in Web API using .Net

Previously we have seen How to Create a record using POSTmethod and Retrieve the data using GET Method. PUT method is used to update some records that already in database. While using PUT method we will be passing Primary Key value in URI, this is considered as Where condition to update the data.

1.      Create new Project with WEB API as below


This will create the new project of WEBAPI in MVC framework.
  
2.      Right Click on the controllers and add new WEB API Controller as shown below.



Name your WEB API controller accordingly, I have named it as EmployeeController.

3.      Right click on the Model folder and add ADO.NET Entity Data Model as shown below.


    Entity Data Model wizard asks you to select what the model should contain, Select EF designer from Database as shown below.   



  Click on Next and Choose Data Connection by selecting new connection.

    
   After Clicking New Connection, it will prompt the new popup to enter server details, userid, password and selecting the database.



After test connection is successful click on OK and your connection string will get generated as shown below




    Select the Entity framework version and Click the Next button.

   


     Select the Table that is necessary for accessing the data as shown below.


Click on Finish that will generate the EDMX file with table name and columns as shown below.



4.      On EmployeeController.cs we can find multiple functions remove all the except PUT method and replace the code as below.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApiPutMethod.Models;

namespace WebApiPutMethod.Controllers
{
    public class EmployeeController : ApiController
    {
        public HttpResponseMessage Put(int id, [FromBody]EmployeeDetail employee)
        {
            try
            {
                using (ACT2_MINIQEntities entities = new ACT2_MINIQEntities())
                {
                  var entity = entities.EmployeeDetails.FirstOrDefault(e => e.ID == id);
                  if (entity != null)
                  {
                      entity.Name = employee.Name;
                      entity.Gender = employee.Gender;
                      entity.City = employee.City;
                      entities.SaveChanges();
                      return Request.CreateResponse(HttpStatusCode.OK, entity);
                  }
                  else
                  {
                      return Request.CreateResponse(
                          HttpStatusCode.NotFound,
                          "Employee ID= " + id + " not found"
                          );
                  }
                }
             
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
            }
        }
    }
}



To Test WEB API we can use fiddler or POSTMAN Chrome Extension 

Output: