using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using WebApplication4.Models;
using System.Data.Entity;
using System.Web.Http.Description;
namespace WebApplication4.Controllers
{
[RoutePrefix("api/Emps")]
public class EmpsController : ApiController
{
[HttpGet]
public async Task<IHttpActionResult> Gets()
{
try
{
using (AppdbContext obj = new AppdbContext())
{
return Ok(await obj.Emps.ToListAsync());
}
}
catch (Exception ex)
{
return StatusCode(HttpStatusCode.InternalServerError);
}
}
[HttpGet]
[ResponseType(typeof(EMP))]
[Route("Get/{id}",Name ="Get")]
public async Task<IHttpActionResult> Get(int Id)
{
try
{
using (AppdbContext obj = new AppdbContext())
{
return Ok(await obj.Emps.FirstOrDefaultAsync(m=>m.Eid==Id));
}
}
catch (Exception ex)
{
return StatusCode(HttpStatusCode.InternalServerError);
}
}
[HttpPost]
[ResponseType(typeof(EMP))]
public async Task<IHttpActionResult> Post(EMP eMP)
{
try
{
using (AppdbContext obj = new AppdbContext())
{
obj.Emps.Add(eMP);
await obj.SaveChangesAsync();
return CreatedAtRoute("Get", new { Id = eMP.Eid }, eMP);
}
}
catch (Exception ex)
{
return StatusCode(HttpStatusCode.InternalServerError);
}
}
[HttpPut]
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> Put(EMP eMP)
{
try
{
using (AppdbContext obj = new AppdbContext())
{
EMP Emp = obj.Emps.Find(eMP.Eid);
Emp.Name = eMP.Name;
Emp.Address = eMP.Address;
await obj.SaveChangesAsync();
return StatusCode(HttpStatusCode.NoContent);
}
}
catch (Exception ex)
{
return StatusCode(HttpStatusCode.InternalServerError);
}
}
[HttpDelete]
[ResponseType(typeof(EMP))]
public async Task<IHttpActionResult> Delete(int Id)
{
try
{
using (AppdbContext obj = new AppdbContext())
{
EMP Emp = obj.Emps.Find(Id);
obj.Emps.Remove(Emp);
await obj.SaveChangesAsync();
return Ok(Emp);
}
}
catch (Exception ex)
{
return StatusCode(HttpStatusCode.InternalServerError);
}
}
}
}
MVC code :
IIn order to create a Web API client.
Add Microsoft.AspNet.Webapi.Client using NuGet package manager.
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class EmpsController : Controller
{
private string Baseurl = string.Empty;
public EmpsController()
{
Baseurl = "http://localhost:55419/api/Emps";
}
[HttpGet]
public ActionResult Index()
{
using (HttpClient webClient = new HttpClient())
{
HttpResponseMessage webResponse = webClient.GetAsync(Baseurl).Result;
return View(webResponse.Content.ReadAsAsync<List<Empvm>>().Result);
}
}
[HttpGet]
public ActionResult Create()
{
return View(new Empvm());
}
[HttpPost]
public ActionResult Create(Empvm empvm)
{
using (HttpClient webClient = new HttpClient())
{
HttpResponseMessage webResponse = webClient.PostAsJsonAsync(Baseurl, empvm).Result;
return RedirectToAction("Index");
}
}
[HttpGet]
public ActionResult Edit(int id)
{
using (HttpClient webClient = new HttpClient())
{
HttpResponseMessage webResponse = webClient.GetAsync(Baseurl + "/Get/" + id).Result;
Empvm empvm1 = webResponse.Content.ReadAsAsync<Empvm>().Result;
Empvm empvm = new Empvm();
empvm.Eid = empvm1.Eid;
empvm.Name = empvm1.Name;
empvm.Address = empvm1.Address;
return View(empvm);
}
}
[HttpPost]
public ActionResult Edit(Empvm empvm)
{
using (HttpClient webClient = new HttpClient())
{
HttpResponseMessage webResponse = webClient.PutAsJsonAsync(Baseurl, empvm).Result;
return RedirectToAction("Index");
}
}
[HttpGet]
public ActionResult Delete(int id)
{
using (HttpClient webClient = new HttpClient())
{
HttpResponseMessage httpResponseMessage = webClient.DeleteAsync(Baseurl + "/" + id).Result;
return RedirectToAction("Index");
}
}
}
}