using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using SoftwayWeb.Models; using System.Diagnostics; using System.Text; namespace SoftwayWeb.Controllers { public class GiriChiudiController : Controller { string apiUrl = string.Empty; string urlBase = string.Empty; string errMes = string.Empty; private readonly IConfiguration _configuration; HttpClient client; public GiriChiudiController(IConfiguration configuration) { client = new HttpClient(); _configuration = configuration; var key = _configuration["ApplicationInsights:rootUrlApi"]; apiUrl = key; } // GET: GiriChiudiController public ActionResult Index() { // return View(); return RedirectToAction("index", "giri"); } public ActionResult Chiudi(string id) { SessionHelper helper = new SessionHelper(this); GiriConsegnaView model = new GiriConsegnaView(); List lst=new List(); //https://api.poloinformatico.it:8000/api/Giri/listaGiri?aperto=true apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "Giri/listaGiri?aperto=true"; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; HttpResponseMessage response = client.GetAsync(baseAddress).Result; if (response.IsSuccessStatusCode) { string data = response.Content.ReadAsStringAsync().Result; lst = JsonConvert.DeserializeObject>(data); model = lst.Where(x => x.SerialeGiro.Equals(id)).First(); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } return View(model); } [HttpPost] public ActionResult Chiudi(GiriConsegnaView model) { //https://api.poloinformatico.it:8000/api/Giri/closeGiro?serialeGiro=0000000002&bancaliRecuperati=11&importoRecuperato=100 SessionHelper helper = new SessionHelper(this); apiUrl = helper.GetStringValue("apiUrl"); urlBase = apiUrl + "Giri/closeGiro?serialeGiro="; urlBase = urlBase + model.SerialeGiro; urlBase = urlBase + "&bancaliRecuperati="; urlBase = urlBase + model.BancaliRecuperati; urlBase = urlBase + "&importoRecuperato="; urlBase = urlBase + model.ImportoRecuperato; Uri baseAddress = new Uri(urlBase); client = new HttpClient(); client.BaseAddress = baseAddress; string data = JsonConvert.SerializeObject(model); StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; if (response.IsSuccessStatusCode) { return RedirectToAction("Index","Giri"); } else { errMes = response.Content.ReadAsStringAsync().Result; helper.SetStringValue("errMsg", errMes); return RedirectToAction("Error"); } } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { SessionHelper helper = new SessionHelper(this); string e = helper.GetStringValue("errMsg"); return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e }); } } }