简体   繁体   中英

How do I create a detailed error page?

I want to create a custom error page to handle all unhandled exceptions.

I want to redirect to it from the Application_Error(object sender, EventArgs e) method located in Global.asax.cs. How can I show in it some detailed information from the exception which throws the application error?

I've done the same thing you are talking about. I created an ErrorPage that displays info to the user. I also created a function that writes the error info into the Event Logs...

For the page, this is what I'm doing. Just put the labels on there somewhere...

protected void Page_Load(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError().GetBaseException();
    this.lblMessage.Text = ex.Message;
    this.lblSource.Text = ex.Source;
    this.lblStackTrace.Text = ex.StackTrace;
    if (AppProperties.AppEnv != AppEnvironment.PROD)
    {
        this.ErrorDetails.Visible = true;
    }
    else
    {
        this.ErrorDetails.Visible = false;
    }
    Utility.LogError();
    Server.ClearError();
}

This is what the LogError function looks like...

public static void LogError()
{
    LogError(HttpContext.Current.Server.GetLastError().GetBaseException());
}

public static void LogError(Exception ex)
{
    EventLog log = new EventLog();
    if (ex != null)
    {
        log.Source = ConfigurationManager.AppSettings["EventLog"].ToString();
        StringBuilder sErrorMessage = new StringBuilder();
        if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null)
        {
            sErrorMessage.Append(HttpContext.Current.Request.Url.ToString() + System.Environment.NewLine);
        }
        sErrorMessage.Append(ex.ToString());
        log.WriteEntry(sErrorMessage.ToString(), EventLogEntryType.Error);
    }
}

You can get the last exception from Server.GetLastError(). Once you have handled the error you can clear it if you want by calling Server.ClearError().

By the way it is considered bad practice to show too much to the end user can prove to be a security vulnerability. Also, be warned, if you redirect rather than returning a 500 HTTP error code various bots will not realise they are causing a crash and keep running the same broken requests on your site. So be sure to use Server.Transfer() rather than Response.Redirect() and set Response.StatusCode = 500.

您可以使用Server.GetLastError()来获取应用程序抛出的异常。

In your page, do something like this:

ErrorLabel.Text = Server.GetLastError();

This assumes C#. You can then redirect from the global.asax file ApplicationError event. There are other ways of handling this, and I do not recommend using the exception message to show the user.

It is almost always a bad idea to show detailed error information to end users. You could end up exposing all sorts of unsuitable information: file names, database credentials, implementation details etc. Log the exception to a safe place (log file, database etc) but do not show it to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM