コード例 #1
0
        public ActionResult Create(ViajerosViewModel viajero)
        {
            using (var client = new HttpClient())

                try
                {
                    string pruebaValor;
                    client.DefaultRequestHeaders.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
                    //client.DefaultRequestHeaders.TryAddWithoutValidation("icSessionId", icSessionId);

                    string message = System.Text.Json.JsonSerializer.Serialize(viajero);
                    message = message.Insert(1, "\"@type\": \"job\",");
                    byte[] messageBytes = System.Text.Encoding.UTF8.GetBytes(message);
                    var    content      = new ByteArrayContent(messageBytes);
                    content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

                    var response = client.PostAsync("http://localhost:8092/api/tm_via_viajeros", content).Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        var result = response.Content.ReadAsStringAsync().Result;
                        pruebaValor = response.RequestMessage.ToString();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            return(View(viajero));
        }
コード例 #2
0
        public ActionResult Edit(ViajerosViewModel viajero)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:8092/api/tm_via_viajeros");


                var putTask = client.PutAsJsonAsync <ViajerosViewModel>("tm_via_viajeros?id=" + viajero.id.ToString(), viajero);
                putTask.Wait();

                var result = putTask.Result;

                if (result.IsSuccessStatusCode)
                {
                    return(RedirectToAction("Index"));
                }
            }
            return(View(viajero));
        }
コード例 #3
0
        public ActionResult Edit(int id)
        {
            ViajerosViewModel viajero = null;

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

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

                    viajero = readTask.Result;
                }
            }

            return(View(viajero));
        }