Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# - 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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...