asp.net-mvc – 在使用WEB API时,如何从POST中提取HttpResponseMessage中的内容?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 在使用WEB API时,如何从POST中提取HttpResponseMessage中的内容?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一个非常典型的CRUD操作将导致一个对象的Id设置一旦持久化。

所以,如果我在接受一个对象的控制器上有Post方法(JSON序列化,比方说)并返回一个HttpResponseMessage,其中HttpStatusCode Created和Content设置为同一个对象,Id从null更新为整数,那么我如何使用HttpClient获取在那个Id值?

它可能很简单,但我看到的只是System.Net.Http.StreamContent。从post方法返回Int更好吗?

谢谢。

更新(以下回答):

一个工作的例子……

namespace TryWebAPI.Models {
    public class YouAreJoking {
        public int? Id { get; set; }
        public string ReallyRequiresFourPointFive { get; set; }
    }
}

namespace TryWebAPI.Controllers {
    public class RyeController : ApiController {
        public HttpResponseMessage Post([FromBody] YouAreJoking value) {
            //Patience simulated
            value.Id = 42;

            return new HttpResponseMessage(HttpStatusCode.Created) {
                Content = new ObjectContent<YouAreJoking>(value,new JsonMediaTypeFormatter(),new MediaTypeWithQualityHeaderValue("application/json"))
            };
        }
    }
}

namespace TryWebApiClient {
    internal class Program {
        private static void Main(string[] args) {
            var result = CreateHumour();
            Console.WriteLine(result.Id);
            Console.ReadLine();
        }

        private static YouAreJoking CreateHumour() {
            var client = new HttpClient();
            var pennyDropsFinally = new YouAreJoking { ReallyRequiresFourPointFive = "yes",Id = null };

            YouAreJoking iGetItNow = null;
            var result = client
                .PostAsJsonAsync("http://localhost:1326/api/rye",pennyDropsFinally)
                .ContinueWith(x => {
                                var response = x.Result;
                                var getResponseTask = response
                                    .Content
                                    .ReadAsAsync<YouAreJoking>()
                                    .ContinueWith<YouAreJoking>(t => {
                                        iGetItNow = t.Result;
                                        return iGetItNow;
                                    }
                );

                Task.WaitAll(getResponseTask);
                return x.Result;
            });

            Task.WaitAll(result);
            return iGetItNow;
        }
    }
}

似乎Node.js受到启发。

解决方法

您可以使用ReadAsAsync< T>

.NET 4(你可以在没有延续的情况下做到这一点)

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>();
原文链接:https://www.f2er.com/aspnet/252004.html

猜你在找的asp.Net相关文章