13

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);
    }
}
jcvandan
  • 14,124
  • 18
  • 66
  • 103

1 Answers1

16

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

Community
  • 1
  • 1
Josh
  • 10,352
  • 12
  • 58
  • 109