I am trying to work through an example using HttpClient
from the ASP.NET Web Api but I am receiving a status code 500 in the response and I don't know why.
I have the following code in a test:
var baseAddress = new Uri("http://localhost:54997");
var config = new HttpSelfHostConfiguration(baseAddress);
var server = new HttpSelfHostServer(config);
((HttpConfiguration)config).Routes.MapHttpRoute(
name: "default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new
{
id = RouteParameter.Optional
});
using (var client = new HttpClient(server))
{
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync("/api/user/getUserDetails").Result;
}
My controller method is like this:
[HttpGet]
[ActionName("getUserDetails")]
public string GetUserDetails()
{
return "hello";
}
The controller action responds fine in Fiddler.
The error seems to be thrown in the Content property of the response.
[System.Net.Http.ObjectContent<System.Web.Http.HttpError>] = {System.Net.Http.ObjectContent<System.Web.Http.HttpError>}
I'm sorry that's not much to go on.
EDIT Please see Darin's answer below. I also put the code in a console application and it works fine - so why not in the test project?
Can someone point me in the right direction of what might be wrong please?