I have an app service that is working as expected in that I can get to pages, log in, and perform a search.
In the event of errors I have the below code in place in the MVC Controller (not an API endpoint)
{
_log.Error("Customer Search Failed", ex);
Response.StatusCode = 500;
return new JsonResult()
{
Data = new { Success = false, Error = "An error occured.", Amount = 0 },
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
ContentEncoding = System.Text.Encoding.UTF8
};
}
Locally, I get the following response along with an HTTP Status Code of 500:
{"Success":false,"Error":"An error occured.","Amount":0}
In Azure, though, I get this response:
The page cannot be displayed because an internal server error has occurred.
I have written a test app to reproduce this in my own Azure environment so that there wouldn't be anything company Azure Resources that could cause this and I got the same behavior. No application gateways, VNets, anything like that. The app service itself is effectively seeing the 500 HTTP Status Code and then sending its own response vs what I want to send.
Does anyone know why this is happening and/or how to prevent this from happening?
The problem was that Azure must set up standard responses for non-200 HTTP Status Codes. This overrode what my Controllers were returning.
I had to explicitly remove the 500 httpHeader via the web.config in order to see the JSON response I was expecting.
<httpErrors errorMode="Detailed">
<remove statusCode="500" />
</httpErrors>