How to get object using Httpclient with response Ok in Web Api

后端 未结 2 547
耶瑟儿~
耶瑟儿~ 2021-01-01 17:11

my web api like

    public async Task RegisterUser(User user)
    {
        //User Implementation here

        return Ok(user);
           


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 17:56

    You can use (depands on what you need), and de-serialize it back to user object.

    await result.Content.ReadAsByteArrayAsync();
    //or
    await result.Content.ReadAsStreamAsync();
    //or
    await result.Content.ReadAsStringAsync();
    

    Fe, if your web api is returning JSON, you could use

    var user = JsonConvert.DeserializeObject( await result.Content.ReadAsStringAsync());
    

    EDIT: as cordan pointed out, you can also add reference to System.Net.Http.Formatting and use:

    await result.Content.ReadAsAsync()
    

提交回复
热议问题