31

There is an action in my ASP.NET MVC controller that returns JSON data with a 400 Bad Request when invalid parameters are passed to the action.

[HttpDelete]
public ActionResult RemoveObject(string id) {
    if(!Validate(id)) {

        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(new { message = "Failed", description = "More details of failure" });
    }
}

This works perfectly running under IIS or with the development test server launched from Visual Studio. After the project has been deployed to Azure the 400 Bad Request comes back without the JSON data. The content type has changed to 'text/html' and 'Bad Request' for the message.

Why is the behavior different under Azure?

Jacob Parker
  • 2,546
  • 18
  • 31

1 Answers1

68

Add the following entry to your 'web.config'.

<system.webServer>
  <httpErrors existingResponse="PassThrough"/>
</system.webServer>

This will allow HTTP errors to pass through un-molested.

dccollie
  • 798
  • 6
  • 8
  • 6
    Are there any security concerns attached to enabling this? – Poul K. Sørensen Aug 19 '13 at 13:48
  • 13
    DEAR GOD. I just spent the last 12 hours trying to figure out why my AJAX requests wouldn't work. I tried everything from adding CORS support to tweaking proxy settings in my Express.js app. This was the simple solution to ensure JSON would get returned properly. – Sahas Katta Feb 27 '14 at 02:25
  • 2
    I had the same problem on an Amazon AWS instance running IIS 8. This fixed the issue I was having and was driving me bonkers. – mcdrummerman Jun 09 '15 at 06:31
  • 3
    Why is this not the default?! Web API appears to pass through by default. – Mark Shapiro Nov 02 '15 at 16:57
  • 1
    @PoulK.Sørensen this answer explains it quite well: http://stackoverflow.com/a/31041696/2901207 – CularBytes May 08 '16 at 16:24
  • 1
    I spent my whole day trying to figure out why it works on my local and why not on remote (AWS VM). Thanks a lot :) – Jawand Singh Apr 16 '18 at 08:44
  • This doesn't work on my case, any thoughts on where should i also be looking at? – sshanzel Jun 07 '20 at 08:37
  • this works for returning json, but then redirection for 404 will not work. how to have both? – Alexander Sep 18 '20 at 13:52