PUT Method
Go to previous
We simply assume that the Web API PUT method conforms to Microsofts philosophy regarding REST webservice methods.
PUT is for Edit, here update of a current Employee object.
Like this:
// PUT: api/Employee/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] Employee Emp)
So we can write the MVC Controller method as follows:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MyAspNetCoreApplication.Models;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
//PUT Corresponds to Edit(Update) of an Employee object.
public async Task
Edit(int id)
{
Employee emp = null;
using (var client = new HttpClient())
{
var MainUrl = "http://" + HttpContext.Request.Host.Value;
client.BaseAddress = new Uri(MainUrl + "/api/employee");
HttpResponseMessage Res = await client.GetAsync("Employee/" + id.ToString());
if (Res.IsSuccessStatusCode)
{
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee object
emp = JsonConvert.DeserializeObject
(EmpResponse);
}
}
return View(emp);
}