Rest – Web API 2 – ApiController.InternalServerError() returns HTTP 400 status code

asp.netasp.net-web-apirest

This Web API action returns an HTTP 500 (Internal Server Error) status code:

public IHttpActionResult Post()
{
    return InternalServerError();
}

But this action returns an HTTP 400 (Bad Request) status code:

public IHttpActionResult Post()
{
    return InternalServerError(new Exception());
}

I would expect both actions to return a 500 status code and the second action puts some of the error's details in the response body.

My first thought is that this is a bug but I wanted to get some other input. Is there any good reason why a 400 should be returned in the second action instead of a 500?

UPDATE:

The documentation on this method reads:

Creates an System.Web.Http.Results.ExceptionResult (500 Internal Server Error) with the specified exception.

I'm thinking more and more this is a bug.

Best Answer

Right, this was a known issue which was fixed after Web API 2 release...you can use the following workaround to fix this issue..example:

return new ResponseMessageResult(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, invalidOpException));

Following was the issue that was logged before:
https://aspnetwebstack.codeplex.com/workitem/1318