我正在检索帐户:
public async Task<JObject> GetAccount(string query)
{
var task = await Client.Instance.GetAsync(Client.Instance.BaseAddress + query);
var jsonString = await task.Content.ReadAsStringAsync();
var account = JsonConvert.DeserializeObject<JObject>(jsonString);
return account;
}
无论Client.Instance.GetAsync...
是返回200还是400或任何其他响应类型,当响应被反序列化为帐户(JObject)
时,响应始终为200。
我们如何从JObject获取响应类型?
答案 0 :(得分:1)
您从内部查询的响应中获取响应类型。该行动基本上充当代理人。使用内部查询的响应来构造传递给调用客户端的响应。
public async Task<IHttpActionResult> GetAccount(string query) {
//get the query response NOTE: assuming it is HttpResponseMessage
var queryResponse = await Client.Instance.GetAsync(Client.Instance.BaseAddress + query);
//get the status code for pass through
var queryResponseStatus = queryResponse.StatusCode;
//content to be passed on in the response
var jsonString = await queryResponse.Content.ReadAsStringAsync();
//create response message with status code
var responseMessage = Request.CreateResponse(queryResponseStatus);
//assign the content of the response
responseMessage.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
//return the result
return ResponseMessage(responseMessage);
}