I am trying to return the appropriate Http codes and responses from my application, but I'm afraid. There seem to be two ways to return specific HTTP responses.
The way I want to deal with this is to throw away HttpResponseException:
public Information Get(int apiKey)
{
if (!_users.Authenticate(apiKey))
{
var response = new HttpResponseMessage();
response.StatusCode = (HttpStatusCode)401;
response.ReasonPhrase = "ApiKey invalid";
throw new HttpResponseException(response);
}
return _info.Get();
}
However, when I do this, the answer I see is just an empty 200 answer!
It also seems that you can also change the signature of your action method to return HttpResponseMessageas follows:
public HttpResponseMessage Get()
{
if (!_users.Authenticate(apiKey))
{
return Request.CreateResponse((HttpStatusCode) 401, "ApiKey invalid");
}
return Request.CreateResponse((HttpStatusCode) 200, _info.Get());
}
I really don't want to do this, if I can help, I would prefer my return type to be the object I'm trying to retrieve, rather than wrapping it every time in HttpResponseMessage.
Is there a reason why the first method returns an empty 200 rather than 401 with a message as I want?