garret garret - 3 months ago 5x
JSON Question

MVC JSON response with bad request

During developing ASP.NET MVC 4 application I encounter problem with returning notification in JSON in my Controllers action. Below I attached code I am using to send notification:

return GetNotificationResult(HttpStatusCode.BadRequest, notification);

private JsonResult GetNotificationResult(HttpStatusCode code, string notification)
Response.StatusCode = (int) code;
return Json(new { Notification = notification });

When I test it on localhost it works as expected (it returns status code BadRequest and body contains JSON with notification).

Hovewer, when i deploy this code as Web App in Azure then it returns just BadRequest without JSON notification.

What can cause different results in invoking this code on cloud vs on localhost?


You can add the following to your web.config

  <httpErrors existingResponse="PassThrough"/>

Documentation on httpErrors

PassThrough: Leaves the response untouched if an existing response exists.