如果打开自定义错误,是否会触发global.asax Application_Error事件?

时间:2012-08-24 14:30:35

标签: c# .net asp.net-mvc

如果您在Web配置中将自定义错误设置为RemoteOnly - 这是否意味着错误时不会触发global.asax - Application_Error中的MVC应用程序级错误事件?

我刚刚注意到,当我的应用程序发生某个错误,并且我正在远程查看该站点时,不会记录任何错误。但是,当我访问服务器上的应用程序并发生相同的错误时,将记录错误。

这是自定义错误配置设置:

<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>

修改

出于对人们的兴趣 - 我最终完全关闭了自定义错误并在Application_Error处理重定向:

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);
    }
}

1 个答案:

答案 0 :(得分:16)

如果未调用Server.ClearError或在Page_Error或Application_Error事件处理程序中捕获错误,则会根据Web.config文件部分中的设置处理错误。

有关详细信息,请参阅this SO question