Peter

Peter

  • NA
  • 14
  • 26.1k

Web Api get complex type

Jan 23 2014 6:21 AM
Hi
 
I have written a small ApiController service in C#, which has a "get" method:
[AcceptVerbs("GET")]
public Product GetProduct(Guid id)
 
If I call this directly in a browser, eg "http://myserver/api/v1/GetProduct/9cfbf113-30a2-4189-b4a8-53fdecca1f81", then I see a result like I'd expect. 
 
But how do I call this endpoint from a c# application? I get an error like:  "no MediaTypeFormatter is available to read an object of type Product from content with media type 'text/javascript'".
 
My code:
var client2 = new HttpClient();
client2.BaseAddress = new Uri("http://myserver");
var resp2 = client2.GetAsync("api/v1/GetProduct/9cfbf113-30a2-4189-b4a8-53fdecca1f81").Result;
Product responsePD2 = null;
if (resp2.IsSuccessStatusCode)
{
  responsePD2 = resp2.Content.ReadAsAsync<Product>().Result;
}
else
{
}
 
How do I convert the returned result to a Product type?
Thanks. 
 
 

Answers (1)