Search code examples
asp.net-mvcazure-web-app-serviceasp.net-mvc-custom-filter

Strange behavior when deploying an Azure Website - ASP.net MVC and Custom Error Filter


I am experiencing a very strange behavior when deploying the ASP.Net website to Azure. The Custom Filter behaves differently when deployed to Azure. The issue is that the JsonResult that is generated in the Custom Exception Filter is not serializing the JSON Object or something in Azure.

This is the result of the custom filter printed in Browser Console in Development:

Object {data: Object, status: 400, config: Object, statusText: "Bad Request"} I can see that the "data" is an object and has the following properties: data: Object details: " stack details ..." message: "Website is required!"

In Azure the same thing is looking as:

Object {data: "Bad Request", status: 400, config: Object, statusText: "Bad Request"} config: Object data: "Bad Request"

I can see that the data is only string!

My custom filter is as below:

public class CustomErrorFilter : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        //No further processing when the exception is handled 
        if (filterContext.ExceptionHandled)
        {
            return;
        }

        if (!(filterContext.Exception is BusinessException)) // Non business exceptions
        {
            //Logging the actual exception
            ElmahLogger.LogError(filterContext.Exception);
            System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);

            //Changing exception type to a generic exception
            filterContext.Exception = new SystemException("An error occurred and logged during processing of this application. We appologise for any in-convieneces.");

            HandleAjaxException(filterContext, HttpStatusCode.InternalServerError);

            filterContext.ExceptionHandled = true;
        }
        else
        {
            System.Diagnostics.Trace.TraceError(filterContext.Exception.Message);

            HandleAjaxException(filterContext, HttpStatusCode.BadRequest);

            filterContext.ExceptionHandled = true;
        }

    }

    private static void HandleAjaxException(ExceptionContext filterContext, HttpStatusCode httpStatusCode)
    {

        filterContext.HttpContext.Response.StatusCode = (int)httpStatusCode;

**// Seems like this is not working in Azure somehow????**
        filterContext.Result = new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Data = new
            {
                message = filterContext.Exception.Message,
                details = filterContext.Exception.StackTrace
            }
        };

        System.Diagnostics.Trace.TraceError("HandleAjaxException" + filterContext.Exception.Message);

    }

}

Can anyone point out what is going on please?


Solution

  • Ok, I finally figured it out. Needed to change the web.config file as IIS8 is not showing the detailed errors by default. This fixed it: adding <httpErrors existingResponse="PassThrough"/> line inside the <system.webServer>.

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

    This will ensure upstream errors are rendered to the browser.