PHP API :
DOT NET MVC:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebApplication9.Models;
using System.Threading.Tasks;
namespace WebApplication9.Controllers
{
public class ClassController : Controller
{
// GET: Class
public async Task<ActionResult> Index()
{
List<ClassVM> result = new List<ClassVM>();
using (var client = new HttpClient())
{
using (var response = await client.GetAsync("http://localhost:8080/CMS/API/Test"))
{
if (response.IsSuccessStatusCode)
{
var productJsonString = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<ClassVM[]>(productJsonString).ToList();
}
}
}
return View(result);
}
[HttpGet]
public ActionResult Create()
{
ClassVM cvm = new ClassVM();
return View(cvm);
}
[HttpPost]
public async Task<ActionResult> Create(ClassVM vm)
{
using (var client = new HttpClient())
{
var serializedProduct = JsonConvert.SerializeObject(vm);
var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json");
var result = await client.PostAsync("http://localhost:8080/CMS/API/Test", content);
}
return RedirectToAction("Index");
}
[HttpGet]
public async Task<ActionResult> Edit(int id)
{
ClassVM result = new ClassVM();
using (var client = new HttpClient())
{
using (var response = await client.GetAsync("http://localhost:8080/CMS/API/Test?CID="+id))
{
if (response.IsSuccessStatusCode)
{
var productJsonString = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<ClassVM>(productJsonString);
}
}
}
return View(result);
}
[HttpPost]
public async Task<ActionResult> Edit(ClassVM vm)
{
using (var client = new HttpClient())
{
var serializedProduct = JsonConvert.SerializeObject(vm);
var content = new StringContent(serializedProduct, Encoding.UTF8, "application/json");
var result = await client.PutAsync("http://localhost:8080/CMS/API/Test", content);
}
return RedirectToAction("Index");
}
[HttpGet]
public async Task<ActionResult> Delete(int id)
{
using (var client = new HttpClient())
{
var result = await client.DeleteAsync("http://localhost:8080/CMS/API/Test?CID="+id);
}
return RedirectToAction("Index");
}
}
}
No comments:
Post a Comment