Home > Back-end >  Azure App Service Hijacks Response for HTTP STATUS 500 Errors
Azure App Service Hijacks Response for HTTP STATUS 500 Errors

Time:01-13

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?

CodePudding user response:

  • Ensure that you don't have a duplicate entry in web.config file.

  • To fetch more on the error you can always review the Application Logs Failed request tracing and/or capture a network trace (F12).

  • Each app has the default root path (/) mapped to D:\home\site\wwwroot, where your code is deployed by default. If your app root is in a different folder, or if your repository has more than one application, you can edit or add virtual applications and directories.

  • Check the Web App's configuration settings.

  • Try adding Application Insights to the app. You should see errors on startup of your application.

  • Enable the Detailed error message functionality through the Azure portal.

enter image description here

  • You should have log files under the directory d:/home/LogFiles/DetailedErrors in your Azure server. You can refer to the link to know how to download them. These files will provide more specific details for the HTTP errors.

Please refer SO Thread for more details.

CodePudding user response:

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>
  •  Tags:  
  • Related