Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 412
jebar8 Avatar asked Nov 20 '13 17:11

jebar8


People also ask

How does Web API handle 400 error?

BadRequest: 400 Error A 400 error, BadRequest, is used when you have validation errors from data posted back by the user. You pass a ModelStateDictionary object to the BadRequest method and it converts that dictionary into JSON which, in turn, is passed back to your HTML page.


1 Answers

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

like image 181
Kiran Avatar answered Sep 21 '22 06:09

Kiran