Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is global.asax Application_Error event not fired if custom errors are turned on?

If you have custom errors set to RemoteOnly in web config - does this mean that MVC's application level error event in global.asax - Application_Error is not fired on error?

I have just noticed that when a certain error occurs in my application, and I am viewing the site remotely, no error is logged. However, when I am accessing the app on the server and the same error occurs, the error is logged.

this is the custom errors config setting:

<customErrors defaultRedirect="/Error/Application" mode="RemoteOnly">
    <error statusCode="403" redirect="/error/forbidden"/>
    <error statusCode="404" redirect="/error/notfound"/>
    <error statusCode="500" redirect="/error/application"/>
</customErrors>

EDIT

Just out of interest for people - I ended up completely turning off custom errors and dealing with redirection in Application_Error like so:

protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();

    // ... log error here

    var httpEx = exception as HttpException;    

    if (httpEx != null && httpEx.GetHttpCode() == 403)
    {
        Response.Redirect("/youraccount/error/forbidden", true);
    }
    else if (httpEx != null && httpEx.GetHttpCode() == 404)
    {
        Response.Redirect("/youraccount/error/notfound", true);
    }
    else
    {
        Response.Redirect("/youraccount/error/application", true);
    }
}
like image 763
jcvandan Avatar asked Aug 24 '12 14:08

jcvandan


1 Answers

If you do not call Server.ClearError or trap the error in the Page_Error or Application_Error event handler, the error is handled based on the settings in the section of the Web.config file.

See this SO question for more information

like image 150
Josh Avatar answered Nov 08 '22 12:11

Josh