In my previous article I explained the HTTP Get and Post methods in the ASP.NET Web API.
This article explains the HTTP Put and Delete methods.
SNO. |
Action |
HTTP Method |
Relative URI |
1 |
Update Exiting Employee |
PUT |
/api/Employee?uid=uid |
2 |
Delete Record from list |
DELETE |
/api/Employee?uid=uid |
Creating Resources HTTP PUT
Note: The method name should start with PUT.
This method takes two parameters, one is UID from the URI of the request and emp is from the request body.
This method calls the Update(Emp) method. This method takes employee as a parameter then finds the employee from the list using the uid of the employee given by the client. Then finds the index of this employee from the list of employees then removes the employee and again adds this employee in the list and returns true or false.
This method updates the employee in the list and creates a response with a successful message and adds in a header of the response and returns the response.
-
-
-
-
-
-
- public HttpResponseMessage PutUpdateEmployee(int uid,Employee emp)
- {
- emp.Uid = uid;
- if (!Update(emp))
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- else
- {
- var response = new HttpResponseMessage();
- response.Headers.Add("Message", "Succsessfuly Updated!!!");
- return response;
- }
- }
-
-
-
-
-
-
- private Boolean Update(Employee emp)
- {
- if (emp == null)
- {
- throw new ArgumentNullException("emp");
- }
- int index = _emp.FindIndex(p => p.Uid == emp.Uid);
- if (index == -1)
- {
- return false;
- }
- _emp.RemoveAt(index);
- _emp.Add(emp);
- return true;
- }
Now In this step I will explain how to consume a HTTP service Web API PUT Method in a console application.
Create a Resource (HTTP PUT)
In this method set the base address of the ASP.NET Web API and sets the accept header to application/json that tells the server to send data in JSON format.
PutAsJsonAsyn: This method serializes the object into JSON format and sends a PUT request to.
Then this method returns a Response object.
- private static HttpResponseMessage ClientPutRequest(string RequestURI, Employee emp)
- {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri("http://localhost:1379/");
- client.DefaultRequestHeaders.Accept.Clear();
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- HttpResponseMessage response = client.PutAsJsonAsync(RequestURI, emp).Result;
- return response;
- }
Add this code to the Main function of program.cs.->Update Record of employee.
In this method enter the employee id that you want to update then set the employee name, address and city if you want to update any field of the employee and call the method created above and pass the employee object as a parameter then receive the response from this method then call the EnsureSuccessStatusCode method. This method throws an exception when the response status in not success.
Then check IsSuccessStatusCode true and false and return Httpresponsemessage with a header.
-
- Console.WriteLine("----------------Update Employee -------------------");
- Console.WriteLine("Enter Employee id which you want to update");
- int id =Convert.ToInt16(Console.ReadLine());
- Console.WriteLine("Enter Employee Name if you want to update name of employee");
- string Emppame = Console.ReadLine();
- Console.WriteLine("Enter Employee Address if you want to update Address of employee");
- string EmpAddress = Console.ReadLine();
- Console.WriteLine("Enter Employee City if you want to update City of employee");
- string EmpCity = Console.ReadLine();
- Employee emp = new Employee() { Name = Emppame, Address = EmpAddress, City = EmpCity };
- HttpResponseMessage responsePutMethod = ClientPutRequest("api/Employee?uid=" + id, emp);
- responsePutMethod.EnsureSuccessStatusCode();
- if (responsePutMethod.IsSuccessStatusCode)
- {
- IEnumerable<string> headerValues = responsePutMethod.Headers.GetValues("Message");
- Console.WriteLine(headerValues.FirstOrDefault().ToString());
- }
Then run your application and see the output.
Delete Resources HTTP DELETE
Note: The method name should start with Delete.
This method takes employee id as a parameter then gets the employee from the list using employee id given by the client and removes the employee from the list then sends a successful message with response to the client.
-
-
-
-
-
-
- public HttpResponseMessage DeleteEmployee(int Uid)
- {
- Employee emp = this.GetEmployee(Uid);
- if (emp == null)
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- _emp.Remove(emp);
- var response = new HttpResponseMessage();
- response.Headers.Add("DeleteMessage", "Succsessfuly Deleted!!!");
- return response;
- }
Now In this step I will explain how to consume a HTTP service Web API DELETE Method in a console application.
Delete a Resource (HTTP Delete)
In this method set the base address of the ASP.NET Web API and set the accept header to application/json that tells the server to send data in JSON Format.
DeleteAsync: This method request web API with employeeid.
- private static HttpResponseMessage ClientDeleteRequest(string RequestURI)
- {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri("http://localhost:1379/");
- client.DefaultRequestHeaders.Accept.Clear();
- HttpResponseMessage response = client.DeleteAsync(RequestURI).Result;
- return response;
- }
Then Press F5 and see the output in the console window.