locked
PUT to web api saves update to DB but returns 500 error in HTTPMessageResponse RRS feed

  • Question

  • User-1202308568 posted

    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

    Tuesday, August 25, 2015 7:06 PM

Answers

  • User1644755831 posted

    Hello teahou,

    public IHttpActionResult GetById(int Id) { try { var data = repo.GetRecipient(Id); return Ok(data); } catch(Exception ex) { return BadRequest(ex.ToString()); } }

    Isn't this just getting the Getting Recipient ? You posted your model using PutAsJsonAsync where is the use of that? you said you have successfully edit the database but I don't see any edit code am I missing something or is this not complete code.

    as far as I can tell you are passing the id in the url.

       string svcEndPoint = string.Format("http://localhost:17305/api/recipient/{0}", model.RecipKey);

    if this url points to the below method. GetById(int Id)  method then why not just use following method.

    using (var client = new HttpClient())
    {
     string svcEndPoint = string.Format("api/recipient/{0}", model.RecipKey); client.BaseAddress = new Uri("http://localhost:17305/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // New code: HttpResponseMessage response = await client.GetAsync(svcEndPoint); if (response.IsSuccessStatusCode) { YourModel data = await response.Content.ReadAsAsync<YourModel>(); } }

    Please see: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client for more understanding.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2015 10:32 PM

All replies

  • User-782957977 posted

    Please post type of data object returned from Web Api.

    Also can you add HttpPut attribute above WebApi action method like this and try

      [HttpPut] 
    public IHttpActionResult GetById(int Id)
            {
                       }

    Tuesday, August 25, 2015 8:34 PM
  • User-1095120877 posted

    Can you try using following ?

    client.PostAsJsonAsync(svcEndPoint, model).Result;
    Tuesday, August 25, 2015 9:59 PM
  • User1644755831 posted

    Hello teahou,

    public IHttpActionResult GetById(int Id) { try { var data = repo.GetRecipient(Id); return Ok(data); } catch(Exception ex) { return BadRequest(ex.ToString()); } }

    Isn't this just getting the Getting Recipient ? You posted your model using PutAsJsonAsync where is the use of that? you said you have successfully edit the database but I don't see any edit code am I missing something or is this not complete code.

    as far as I can tell you are passing the id in the url.

       string svcEndPoint = string.Format("http://localhost:17305/api/recipient/{0}", model.RecipKey);

    if this url points to the below method. GetById(int Id)  method then why not just use following method.

    using (var client = new HttpClient())
    {
     string svcEndPoint = string.Format("api/recipient/{0}", model.RecipKey); client.BaseAddress = new Uri("http://localhost:17305/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // New code: HttpResponseMessage response = await client.GetAsync(svcEndPoint); if (response.IsSuccessStatusCode) { YourModel data = await response.Content.ReadAsAsync<YourModel>(); } }

    Please see: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client for more understanding.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2015 10:32 PM