I am having a problem with 500 error handling, working in VB.NET.
My problem is that when a 500 error is thrown, my site redirects to my error page. But if I inspect the Network tab in Chrome, I see that I get a 200 (OK) for the page that threw the error and then another 200 for the page that was redirected to.
Am I right in thinking that a 500 error should be returned on the page that throws the error, and a 200 returned for the redirected page?
My web config’s error handling is set up as follows:
<customErrors defaultRedirect="~/error.aspx" mode="On">
<error statusCode="400" redirect="~/400Error.aspx" />
<error statusCode="500" redirect="~/error.aspx?code=500" />
</customErrors>
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear />
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/error.aspx" responseMode="ExecuteURL" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" prefixLanguageFilePath="" path="/error.aspx?code=500" responseMode="ExecuteURL" />
<remove statusCode="400" subStatusCode="-1" />
<error statusCode="400" prefixLanguageFilePath="" path="/400Error.aspx" responseMode="ExecuteURL" />
</httpErrors>
But this alone results in the problem I describe above.
I thought that maybe I could return a 500 on the page by using either Application_Error in the Global.asax file:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Response.StatusCode = System.Net.HttpStatusCode.InternalServerError
Server.ClearError()
Server.Transfer("error.aspx?code=500")
End Sub
Or on the page level by adding similar code in my Master Page:
Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error
Response.StatusCode = System.Net.HttpStatusCode.InternalServerError
Server.ClearError()
Server.Transfer("error.aspx?code=500")
End Sub
By catching the error on the page level I am redirected to the error page with the same results as produced by handling the error in the web.config file. Whereas catching the error on the Application level seems to return the error page without doing a redirect(?), and just a single 200 is returned in this instance.
I have tried a combination of the code above – I understand that the Server.ClearError() will stop the error from ‘bubbling up’ to the web.config level? But I have tried with and without that line without much success.
Could anyone tell me if it would be correct to return the 500 on the page that throws the error and if so, what I can do to achieve this?
I found that if I throw an exception at the application level, then I achieve the result of returning the error page without redirecting, as well as returning the 500 Error:
I am not sure if this is the best way of solving my problem but will leave up in case anyone else finds this useful.