いくつかのWebAPIを作成しましたが、エラーが発生すると、APIはCreateErrorResponseメッセージで作成されたHttpResponseMessageを返します。このようなもの:
return Request.CreateErrorResponse(
HttpStatusCode.NotFound, "Failed to find customer.");
私の問題は、コンシューマーアプリケーションでメッセージ(この場合は「顧客が見つかりませんでした。 」)を取得する方法がわからないことです。
消費者のサンプルは次のとおりです。
private static void GetCustomer()
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
string data =
"{\"LastName\": \"Test\", \"FirstName\": \"Test\"";
var content = new StringContent(data, Encoding.UTF8, "application/json");
var httpResponseMessage =
client.PostAsync(
new Uri("http://localhost:55202/api/Customer/Find"),
content).Result;
if (httpResponseMessage.IsSuccessStatusCode)
{
var cust = httpResponseMessage.Content.
ReadAsAsync<IEnumerable<CustomerMobil>>().Result;
}
}
どんな助けでも大歓迎です。