【问题标题】:Get Values from HttpRequestException in another application从另一个应用程序中的 HttpRequestException 获取值
【发布时间】:2018-05-23 18:02:58
【问题描述】:

我从一个 Web 应用程序发送 Exception 并在另一个应用程序中获取它。 在这种情况下如何从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;
}

【问题讨论】:

    标签: c# model-view-controller httpresponsemessage


    【解决方案1】:

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

     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;
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 2012-11-16
      相关资源
      最近更新 更多