Consuming web api from mvc controller:
[HttpPost] public ActionResult Edit(Recipient model) { string svcEndPoint = string.Format("http://localhost:17305/api/recipient/{0}", model.RecipKey); using (HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true })) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.PutAsJsonAsync(svcEndPoint, model).Result; if (response.IsSuccessStatusCode) { return View(model); } else { return View("Error"); } } }
Web api:
public IHttpActionResult GetById(int Id) { try { var data = repo.GetRecipient(Id); return Ok(data); } catch(Exception ex) { return BadRequest(ex.ToString()); } }
It successfully saves edits to the database, but the response.IsSuccessStatusCode is always false. Even though the web api returns the OK message, the MVC client response is 500 internal server error.
I have tried using Fiddler to see what is happening, but I can't figure out how to get it to grab the response from the web api.
I am new to fiddler and not quite sure if this means anything, but it looks like when it first hits, it returns a 401 error, then it goes through. Could this be the issue?
EDIT: All my gets are working fine and returning status 200, even though they initially show a 401 in fiddler, so I am thinking that is not it.
Here is the header value from a PUT:
POST /Recipient/Edit?RecipientId=13971 HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost:22222/Recipient/Edit?RecipientId=13971
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: localhost:22222
DNT: 1
Cookie: __RequestVerificationToken=Wa8gRZ_K9qubfUjW-6Xe9FzIzpvlIyOvpUexg7pUoQyJllhgCLrdjksDYXD6LbH2OJzUSTLhI-1PlaFxA2rtpa6zVaeD6FIfSRMH4CYdYKw1
Pragma: no-cache
Authorization: Negotiate oXcwdaADCgEBoloEWE5UTE1TU1AAAwAAAAAAAABYAAAAAAAAAFgAAAAAAAAAWAAAAAAAAABYAAAAAAAAAFgAAAAAAAAAWAAAABXCiOIGAbEdAAAAD2mBHrLRKFFkEpDWr0higvGjEgQQAQAAAIU/xffX1AcoAAAAAA==
Content-Length: 306