Get response from PostAsJsonAsync

后端 未结 5 2006
清歌不尽
清歌不尽 2020-12-08 07:47

I have this line of code

var response = new HttpClient().PostAsJsonAsync(posturi, model).Result;

The Called WebAPI controller returns a bo

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 08:15

    I used HttpStatusCode to check the result.

        public HttpStatusCode PostStaffPositions(Foo foo)
        {
            string uri = myapiuri;
    
            using (HttpClient httpClient = new HttpClient())
            {
                var response = httpClient.PostAsJsonAsync(uri, foo).Result;
                return response.StatusCode;
            }
        }
    

    And then in Controller check it like this:

       HttpStatusCode update = staffrest.PostStaffPositions(foo);
                if (update == HttpStatusCode.OK)
                {
                   //Update Succeed
                }
                else
                {
                    //Update Failed
                }
    

提交回复
热议问题