Debugging 400 Bad Request response

matk picture matk · Feb 26, 2015 · Viewed 7.7k times · Source

In my MVC application, when a POST request is sent to the server (via jQuery) and a validation error occurs, a 400 Bad Request is returned, as intended:

HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
Date: Thu, 26 Feb 2015 08:35:50 GMT
Content-Length: 174

{"ReturnValue":null,"Results":[{"Message":"The xyz field is required.","ErrorNumber":123,"Severity":1}]}

This works as intended on my local machine. However when I deploy the application to the server, the response for the exact same request looks different:

HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Date: Thu, 26 Feb 2015 08:37:01 GMT
Content-Length: 24

Invalid Request

Note that the content type is text/html, and the response body doesn't contain JSON anymore.

What could be the cause of this? I would appreciate a pointer where to start debugging.

Answer

matk picture matk · Feb 26, 2015

After further searching and reading I found the answer in this post which also refers to the helpful article IIS 7 Error Pages taking over 500 Errors

the problem was IIS taking over 400 Bad Request errors and replacing my custom JSON response with IIS error content. This behavior can be disabled using the TrySkipIisCustomErrors setting:

Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 400;