Skip to main content

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:




Comments

  1. Thanks for sharing valuable information. Your blogs were helpful to Dot NET learners. I request to update the blog through step-by-step. Also, find the Dot net news at Dot NET Online Course Hyderabad

    ReplyDelete
  2. When I was facing problems, I read your articles. I want to enjoy more and more about your writing. I wish you best things in life.
    free game Battle for the Galaxy Game Break The Cup free game Toon Cup 2017

    ReplyDelete
  3. This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance knowledge. I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got good knowledge.
    WordPress website development Chennai

    ReplyDelete
  4. Very good information provided, Thanks a lot for sharing such useful information.

    DevOps Training in Chennai
    Splunk Training in Chennai

    ReplyDelete
  5. Since Saturday I decided to take my training for the next few weeks into my own hands as the current training and nutrition style was a little too much. I have 15 weeks to go, so not feeling the need to be so drained so far out.
    kizi Games online
    friv jogos gratis
    play friv Games

    ReplyDelete

Post a Comment