Web API 2 - ApiController.InternalServerError () returns an HTTP 400 status code

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

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

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

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

I would expect both actions to return a 500 status code, and the second action will add some error details to the response body.

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

UPDATE:

The documentation for this method reads:

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

I think this is a mistake more and more.

+7
rest asp.net-web-api
source share
1 answer

That's right, it was a known issue that was fixed after the release of Web API 2 ... you can use the following workaround to fix this problem .. Example:

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

Below was a problem that was registered earlier:
https://aspnetwebstack.codeplex.com/workitem/1318

+12
source

All Articles