How to debug my code to be able to read my api

Viewed 25

I am trying to consume my web api from a MVC controller but I keep receiving the error message from the client side saying "failed to load resource the server responded with a status of 500".

[HttpPost]
        public async Task<Client> AddUpdateClient(Client _client)
        {
            customer = new Client();
            string apiRes = "";

            using (var client = new HttpClient(clientHandler))
            {
                client.Timeout = TimeSpan.FromMinutes(5);

                var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
                var refNum = new char[12];
                var random = new Random();

                for (int i = 0; i < refNum.Length; i++)
                {
                    refNum[i] = chars[random.Next(chars.Length)];
                }
                _client.ClientNo = new string(refNum).ToUpper();

                var oClientNo = _client.ClientNo;

                StringContent cntClient = new StringContent(JsonConvert.SerializeObject(_client), Encoding.UTF8, "application/json");

                using (var result = await client.PostAsync("http://localhost:35298/api/Client/", cntClient))
                {
                    apiRes = await result.Content.ReadAsStringAsync();
                    customer = JsonConvert.DeserializeObject<Client>(apiRes);
                }

                HttpContext.Session.SetString("Firstname", _client.Firstame);
                HttpContext.Session.SetString("Lastname", _client.Lastname);
                HttpContext.Session.SetString("RefNum", oClientNo);
                _toastNotification.AddSuccessToastMessage("Successfully success");

                return customer;//RedirectToAction("Index", "Document");
            }
        }
0 Answers
Related