Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Response.StatusCode set to 200 when Application_Error() is invoked?

In my MVC application I sometimes have unhandled exceptions and Application_Error() handler invoked.

The problem is if I access Response.StatusCode inside Application_Error() it happens to be 200 although there was an unhandled exception. I'd rather expect 500.

Why is it 200 although there was an unhandled exception?

like image 630
sharptooth Avatar asked Nov 11 '22 22:11

sharptooth


1 Answers

inside Application_Error() you may do this:

        var lastError = Server.GetLastError();

        var statusCode = 500;
        var httpException = lastError as HttpException;
        if (httpException != null)
            statusCode = httpException.GetHttpCode();
like image 196
razon Avatar answered Nov 15 '22 11:11

razon