【发布时间】:2018-12-04 14:51:20
【问题描述】:
我是 web api 的新手。我有一个方法,它从响应消息中返回 3 个对象。我想从响应消息中获取特定对象,
public HttpResponseMessage GetAllStudents(HttpReqestMessage request)
{
HttpResponseMessage response = null;
return CreateHttpResponse(request, () =>
{
// some logics here
response = request.CreateResponse(HttpStatusCode = OK, new {success = true, StudentName, ListOfStudents, ListOfSubjects});
return response;
});
}
在上面的代码中,我想从响应消息中单独获取 ListOfStudents 对象。请任何人帮助我得到这个。
【问题讨论】:
-
您是说您只想在这段代码中获取该对象吗?还是在调用此服务的代码中?如果是后者,该代码在哪里?响应格式是什么?您目前如何阅读该回复?
-
这感觉就像XY problem。
CreateHttpResponse是做什么的?我们怎么知道它会返回什么? -
试试这个:
response.Content.ReadAsStringAsync().Result,你会得到一个 json 字符串作为回报。接下来尝试反序列化 json 字符串:stackoverflow.com/questions/4611031/… -
我猜你可以使用另一个
ReadAs...Async(),但不确定是哪个。
标签: c# asp.net-mvc asp.net-web-api