POST Method

Go to previous
We simply assume that the Web API POST method conforms to Microsoft philosophy ragarding REST webservice methods. POST is for creating a new Employee object Like this:
// POST: api/Employee [HttpPost] public void Post([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; //POST Corresponds to add a new item of an Employee object. [HttpPost] public async Task Create(Employee employee) { Employee emp = null; using (var client = new HttpClient()) { var MainUrl = "http://" + HttpContext.Request.Host.Value; client.BaseAddress = new Uri(MainUrl + "/api/employee"); //HTTP POST HttpResponseMessage Res = await client.PostAsJsonAsync("Employee", employee); if (Res.IsSuccessStatusCode) { var EmpResponse = Res.Content.ReadAsStringAsync().Result; //Deserializing the response recieved from web api and storing into the Employee object return RedirectToAction("Created"); } } return View(); }