How do I extract content from HttpResponseMessage from POST when using WEB API?

后端 未结 1 637
粉色の甜心
粉色の甜心 2020-12-24 07:26

A pretty typical CRUD operation will result in an object having its Id set once persisted.

So if I have Post method on the controller which accepts an object (JSON

相关标签:
1条回答
  • 2020-12-24 07:57

    You can use ReadAsAsync<T>

    .NET 4 (you can do that without continuations as well)

    var resultTask = client.PostAsJsonAsync<MyObject>("http://localhost/api/service",new MyObject()).ContinueWith<HttpResponseMessage>(t => {
        var response = t.Result;
        var objectTask = response.Content.ReadAsAsync<MyObject>().ContinueWith<Url>(u => {
            var myobject = u.Result;
            //do stuff 
        });
    });
    

    .NET 4.5

        var response = await client.PostAsJsonAsync<MyObject>("http://localhost/api/service", new MyObject());
        var myobject = await response.Content.ReadAsAsync<MyObject>();
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题