3

Exception从一个 Web 应用程序发送并在另一个应用程序中获取它。HttpRequestException在这种情况下如何获取值?

发送自:

context.Response = new HttpResponseMessage(apiError.StatusCode)
{
    Content = new ObjectContent<ApiError>(apiError,
       new JsonMediaTypeFormatter(), @"application/json")
};
return context;

进入另一个:

catch (HttpRequestException e)
{
    this.logger.LogError(
      string.Format("Ошибка запроса {0}", requestUri.AbsoluteUri),
      LogCategoryRepository.ApiCashdesk,
      e);
    throw;
}
4

1 回答 1

0

好的。您只需创建一个类:

 public class ErrorDetails
 {
    public string Details { get; set; }
 }

并使用它:

try
            {
                using (var response = await client.SendAsync(request).ConfigureAwait(false))
                {
                    if(!response.IsSuccessStatusCode)
                    {
                        ***var error = await response.Content.ReadAsAsync<ErrorDetails>().ConfigureAwait(false);***
                        throw new HttpRequestException(error.Details);
                    }
                    response.EnsureSuccessStatusCode();
                    var result = await response.Content.ReadAsAsync<TResponse>().ConfigureAwait(false);
                    return result;
                }
            }
于 2018-05-26T17:30:51.407 回答