从 .NET 4.5 控制台应用程序使用 MVC 4.0 Web API 时出现以下错误。“没有 MediaTypeFormatter 可用于从媒体类型为 'text/html' 的内容中读取类型为 'IEnumerable`1' 的对象。” 请帮忙!下面是我的代码:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:30151/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//List all Customers
HttpResponseMessage response = client.GetAsync("api/customers").Result;
if (response.IsSuccessStatusCode)
{
var customers = response.Content.ReadAsAsync<IEnumerable<Customer>>().Result;
foreach (var c in customers)
{
Console.WriteLine("ID: {0}\tName: {1}\tAddress: {2}\tEmail: {3}\tPhone: {4}", c.id, c.name, c.address, c.email, c.phone);
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
Console.Read();