WebAPI:如何从客户端的HttpResponseMessage类读取消息

时间:2016-08-26 13:33:42

标签: asp.net-web-api

下面的

是一个返回HttpResponseMessage的示例操作,如果发生任何错误,那么web api操作会将错误消息和状态代码返回给客户端返回Request.CreateErrorResponse(HttpStatusCode.NotFound, message);

[HttpGet, Route("GetAll")]
public HttpResponseMessage GetAllCustomers()
{
    IEnumerable<Customer> customers = repository.GetAll();
    if (customers == null)
    {
        var message = string.Format("No customers found");
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
    }
    else
    {
        return Request.CreateResponse(HttpStatusCode.OK, customers);
    }
}

当我通过http客户端调用操作时,我没有在ReasonPhrase属性中收到消息。告诉我在客户端读取消息的正确方法是什么,这样就是这样传递return Request.CreateResponse(HttpStatusCode.OK, customers);

这是我的客户端代码

private async void btnFind_Click(object sender, EventArgs e)
{
    var fullAddress = baseAddress + "api/customer/GetByID/"+txtFind.Text;
    Customer _Customer = null;

    using (var client = new HttpClient())
    {
        using (var response = client.GetAsync(fullAddress).Result)
        {
            if (response.IsSuccessStatusCode)
            {
                var customerJsonString = await response.Content.ReadAsStringAsync();
                _Customer = JsonConvert.DeserializeObject<Customer>(customerJsonString);
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }
        }
    }

    if (_Customer != null)
    {
        var _CustList = new List<Customer> { _Customer };
        dgCustomers.DataSource = _CustList;
    }

}

response.ReasonPhrase没有抓住我从行动中传递的信息。所以我可能不会做读取消息的事情。请告诉我在我的代码中要更改的内容以阅读该消息。感谢

1 个答案:

答案 0 :(得分:0)

我有这样的工作。

private async void btnFind_Click(object sender, EventArgs e)
{
    var fullAddress = baseAddress + "api/customer/GetByID/"+txtFind.Text;
    Customer _Customer = null;
    try
    {
        using (var client = new HttpClient())
        {
            using (var response = client.GetAsync(fullAddress).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    var customerJsonString = await response.Content.ReadAsStringAsync();
                    _Customer = JsonConvert.DeserializeObject<Customer>(customerJsonString);
                }
                else
                {
                    //Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                    var ErrMsg = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
                   MessageBox.Show(ErrMsg.Message);
                }
            }
        }

        if (_Customer != null)
        {
            var _CustList = new List<Customer> { _Customer };
            dgCustomers.DataSource = _CustList;
        }
    }
    catch (HttpRequestException ex)
    {
        // catch any exception here
    }
}

以这种方式读取错误消息。

var ErrMsg = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);