|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using MVC.Models; |
| 3 | +using Newtonsoft.Json; |
| 4 | +using System.Security.Cryptography.Xml; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace MVC.Controllers |
| 8 | +{ |
| 9 | + public class StudentController : Controller |
| 10 | + { |
| 11 | + private string url="https://localhost:7287/api/StudentAPI"; |
| 12 | + |
| 13 | + private HttpClient client = new HttpClient(); |
| 14 | + |
| 15 | + [HttpGet] |
| 16 | + public IActionResult Index() |
| 17 | + { |
| 18 | + List<Student> students = new List<Student>(); |
| 19 | + HttpResponseMessage response = client.GetAsync(url).Result; |
| 20 | + if (response.IsSuccessStatusCode) |
| 21 | + { |
| 22 | + string result = response.Content.ReadAsStringAsync().Result; |
| 23 | + var data = JsonConvert.DeserializeObject<List<Student>>(result); |
| 24 | + if (data!=null) |
| 25 | + { |
| 26 | + students = data; |
| 27 | + |
| 28 | + } |
| 29 | + } |
| 30 | + return View(students); |
| 31 | + } |
| 32 | + [HttpGet] |
| 33 | + public IActionResult Create() |
| 34 | + { |
| 35 | + return View(); |
| 36 | + } |
| 37 | + |
| 38 | + [HttpPost] |
| 39 | + public IActionResult Create(Student std) |
| 40 | + { |
| 41 | + string data = JsonConvert.SerializeObject(std); |
| 42 | + StringContent content = new StringContent(data,Encoding.UTF8,"application/json"); |
| 43 | + HttpResponseMessage response = client.PostAsync(url, content).Result; |
| 44 | + if (response.IsSuccessStatusCode) |
| 45 | + { |
| 46 | + TempData["insert_message"] = "data added...."; |
| 47 | + |
| 48 | + return RedirectToAction("index"); |
| 49 | + } |
| 50 | + return View(); |
| 51 | + } |
| 52 | + [HttpGet] |
| 53 | + public IActionResult Edit(int id) |
| 54 | + { |
| 55 | + Student std = new Student(); |
| 56 | + HttpResponseMessage response = client.GetAsync(url + id).Result; |
| 57 | + if (response.IsSuccessStatusCode) |
| 58 | + { |
| 59 | + string result = response.Content.ReadAsStringAsync().Result; |
| 60 | + var data = JsonConvert.DeserializeObject<Student>(result); |
| 61 | + if (data != null) |
| 62 | + { |
| 63 | + std = data; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return View(std); |
| 68 | + } |
| 69 | + [HttpPost] |
| 70 | + public IActionResult Edit(Student std) |
| 71 | + { |
| 72 | + string data = JsonConvert.SerializeObject(std); |
| 73 | + StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); |
| 74 | + HttpResponseMessage response = client.PutAsync(url+std.id, content).Result; |
| 75 | + if (response.IsSuccessStatusCode) |
| 76 | + { |
| 77 | + TempData["update_message"] = "student updated...."; |
| 78 | + |
| 79 | + return RedirectToAction("index"); |
| 80 | + } |
| 81 | + return View(); |
| 82 | + } |
| 83 | + [HttpGet] |
| 84 | + public IActionResult Details(int id) |
| 85 | + { |
| 86 | + Student std = new Student(); |
| 87 | + HttpResponseMessage response = client.GetAsync(url + id).Result; |
| 88 | + if (response.IsSuccessStatusCode) |
| 89 | + { |
| 90 | + string result = response.Content.ReadAsStringAsync().Result; |
| 91 | + var data = JsonConvert.DeserializeObject<Student>(result); |
| 92 | + if (data != null) |
| 93 | + { |
| 94 | + std = data; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return View(std); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments