Unable to test the post, put and delete method via postman. "get method via id and all employees is working"

  • Thread starter Thread starter _RJ_
  • Start date Start date
R

_RJ_

Guest
Unable to test the post, put and delete method via postman. "get method via id and all employees is working"

I am trying to create a test api call. I have creates a mvc form and api page for the processes like get, put post and delete.
I am facing error when trying to test the same from postman application.

{ "message": "The requested resource does not support http method 'POST'." }

Sharing the code for home controller and employee controller.


using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Mvc;
using WebApplication2Test.Database;
using WebApplication2Test.Models;

namespace WebApplication2Test.Controllers
{
public class HomeController : Controller
{

private EmployeeDbEntities db = new EmployeeDbEntities();
// GET: Employee

public ActionResult Index()
{
IEnumerable<EmployeeViewModel> employees = null;

using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
{
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
client.BaseAddress = new Uri(baseUrl);

var apiMethod = $"api/employees";

var response = client.GetAsync(apiMethod);

var result = response.Result;

if (result.IsSuccessStatusCode)
{

var test = result.Content.ReadAsStringAsync();
test.Wait();

var apiResponseData = test.Result;

employees = JsonConvert.DeserializeObject<IEnumerable<EmployeeViewModel>>(apiResponseData);


}

return View(employees);
}
}


////GET: Employee/Details/5
//public ActionResult Details(int id)
//{
// var employee = db.Employees.SingleOrDefault(e => e.ID == id);
// return View(employee);
//}

// GET: Employee/details/5
public ActionResult Details(int id)
{
EmployeeViewModel employee = CallApiGetEmployeeMethod(id);

return View(employee);

}

private EmployeeViewModel CallApiGetEmployeeMethod(int id)
{

EmployeeViewModel employee = null;

using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true }))
{
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
client.BaseAddress = new Uri(baseUrl);

var apiMethod = $"api/employees/" + id;

var response = client.GetAsync(apiMethod);

var result = response.Result;

if (result.IsSuccessStatusCode)
{

var test = result.Content.ReadAsStringAsync();
test.Wait();

var apiResponseData = test.Result;

employee = JsonConvert.DeserializeObject<EmployeeViewModel>(apiResponseData);


}

return employee;
}

}

//GET: Employee/Create
public ActionResult Create()
{
return View();
}

//POST: Employee/Create
[HttpPost]
public ActionResult post(EmployeeViewModel employee)
{
try
{
using (var client = new HttpClient())
{

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
client.BaseAddress = new Uri(baseUrl);

var apiMethod = $"api/employees/createemployee";

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var postTask = client.PostAsJsonAsync<EmployeeViewModel>(apiMethod, employee);

postTask.Wait();

var result = postTask.Result;

if(result.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
return View();
}

}
catch (Exception)
{
return View();
}
}

// GET: Employee/Edit/5
public ActionResult Edit(int id)
{
EmployeeViewModel employee = CallApiGetEmployeeMethod(id);
return View(employee);
}

// public ActionResult Put(int id)
// {
// EmployeeViewModel employee = null;

// using (var client = new HttpClient())
// {
// client.BaseAddress = new Uri("https://localhost:44316//api/");
// //HTTP GET
// var responseTask = client.GetAsync("Employee?id=" + id.ToString());
// responseTask.Wait();

// var result = responseTask.Result;
// if (result.IsSuccessStatusCode)
// {
// var readTask = result.Content.ReadAsAsync<EmployeeViewModel>();
// readTask.Wait();

// Employee = readTask.Result;
// }
// }

// return View(employee);
// }
//}


// POST: Employee/Edit/5
[HttpPost]
public ActionResult Edit(int id, EmployeeViewModel employee)
{
try
{

if (TryUpdateModel(employee))
{

using (var client = new HttpClient())
{

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
client.BaseAddress = new Uri(baseUrl);

var apiMethod = $"api/employees/updateemployee";

client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

//HTTP Put
var putTask = client.PutAsJsonAsync<EmployeeViewModel>(apiMethod, employee);
//var putTask = client.PutAsync(apiMethod);

putTask.Wait();

var result = putTask.Result;
if (result.IsSuccessStatusCode)
{

return RedirectToAction("Index");
}
}
}

return View(employee);
}
catch
{
return View();
}
}


// GET: Employees/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}


using (var client = new HttpClient())
{

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
client.BaseAddress = new Uri(baseUrl);

var apiMethod = $"api/employees/" + id.ToString();

//HTTP DELETE
var deleteTask = client.DeleteAsync(apiMethod);

deleteTask.Wait();

var result = deleteTask.Result;
if (result.IsSuccessStatusCode)
{

return RedirectToAction("Index");
}
}

return RedirectToAction("Index");

}

// POST: Employee/Delete/5
[HttpDelete]
public ActionResult Delete(int id)
{
Employee employee = db.Employees.Find(id);
db.Employees.Remove(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
// catch
//{
// return View();
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}

base.Dispose(disposing);
}
}
}

Continue reading...
 
Back
Top