Error Handling in ASP.NET Core Blazor and Web Forms
- 7 minutes to read
This topic demonstrates error handling mechanisms used in ASP.NET Core Blazor and ASP.NET Web Forms applications and explains how to customize them.
#Error Handling in ASP.NET Core Blazor
XAF ASP.NET Core Blazor applications support error handling out of the box.
XAF displays toast notifications for errors that implement IUserFriendlyException
. The following image shows an example with validation error messages:
XAF displays a pop-up window in debug mode with detailed error information for errors that occur in Views and their toolbars (for example, when XAF cannot render a View):
For security, the pop-up window does not contain the stack trace in production mode.
When such errors occur, we recommend that you click Refresh Page to refresh the browser window and load a new application instance that does not contain incorrect data. In this case, you lose unsaved changes.
To save your changes before you reload the application, click Cancel or Close to hide the pop-up window. Copy unsaved data. We do not recommend that you continue working with a corrupted application state. Reload the app to avoid additional errors.
Click Copy Details to copy the error message for debugging.
In case of a critical error, XAF displays a full-screen error page. For example, the following page may appear on application startup if XAF has written incorrect data to the database:
#Error Handling in ASP.NET Web Forms
#Error Messages on the Current Page (ErrorInfoControl)
In XAF ASP.NET Web Forms applications, an exception can be raised as a result of executing an Action. In this case, there is no need to load an error page, which could confuse the end-user. Instead, the XAF provides the ErrorInfoControl control displayed on the current page. All the Web Templates contain this control, but it is initially invisible. When an exception is raised in an Action’s event handler, the system that handles exceptions makes this control visible, and displays the error description in it.
The following image illustrates the ErrorInfoControl on a page with the failed Action.
After this, an end-user can continue working with the application.
You can also use this ErrorInfoControl to manually provide information on non-critical errors. For this purpose, call the ErrorHandling.SetPageError method, which takes the required exception as a parameter. For example, built-in Action Containers call this method when an exception is raised while executing an Action from their Action collections.
public static class ActionExceptionHandler {
public static void HandleException(HandleExceptionEventArgs e) {
if(!e.Handled) {
ErrorHandling.Instance.SetPageError(e.Exception);
e.Handled = true;
}
}
}
// ...
public class ActionContainerHolder : Panel, INamingContainer {
// ...
private void action_ExecutionException(object sender, HandleExceptionEventArgs e) {
ActionExceptionHandler.HandleException(e);
}
// ...
}
In the code above, the ErrorHandling is a class that is used to handle errors occurring in ASP.NET Web Forms applications.
Note
You can handle the static Error
#Error Messages on Error Pages
Errors that are not reported via the ErrorInfoControl (defined above) are reported on error pages. Error pages are shown by the Application_Error method implemented in the Web Application project’s Global.asax.cs (Global.asax.vb) file. This method invokes the ErrorHandling.ProcessApplicationError method, which performs the following.
Sends Notification to the Application Administrator
Error description and the corresponding section from the log file are sent by e-mail to recipients specified in the Web.config file. Available e-mail settings are listed in the end of this topic.
XAF sends collected error information via the Simple Mail Transfer Protocol (SMTP). To use another notification delivery mechanism (such as instant messaging), handle the ErrorHandling.CustomSendErrorNotification event. Implement notification delivery and set the
Handled
parameter totrue
. If you use SMTP, but need to customize message options (for example, provide SMTP credentials), use the <smtp> element or handle the ErrorHandling.CustomSendMailMessage event. You can also write custom code that sends the message. In this instance, set theHandled
parameter totrue
.Writes Exception to Log File
Information on the occurred exception is logged.
Displays Error Page
Normally, the Error.aspx page specified by the RichErrorReportPage setting is loaded. This page provides the error description and possible steps to be performed by an end-user. In addition, if the application was run locally, or the user IP was specified by the DetailedErrorReaderIp setting, then this error page would contain detailed information on the error and the corresponding section of the log file. The error page can also include an editor that allows end-users to post additional information to application administrators. The editor is not displayed if e-mail settings are not configured.
If the error has occurred when you start the application, or the error page is not specified by the RichErrorReportPage setting, an HTML page with an error message is generated. You can specify a custom HTML page to be loaded in this situation. For this purpose, set the SimpleErrorReportPage setting to the custom page. The {REQUESTURL} and {DETAILS} parameters are allowed in this key’s value. These parameters will be substituted with the request URL and error details respectively.
The following diagram illustrates the error handling mechanism.
The following image demonstrates the default simple error page.
The following image demonstrates the default rich error page.
#Configuration File Keys Related to the Error Handling
Key Name | Sample Value | Description |
---|---|---|
Error |
admin@example. |
Specifies the e-mail address the error notifications will be sent to. You can provide several addresses, separated by semicolons. If omitted, the notifications are not sent. |
Error |
smtp. |
Specifies the SMTP server used to deliver notifications. If the server requires authentication, follow the instructions from the Error |
Error |
application@example. |
Specifies the e-mail address the error notifications will be sent from. If omitted, the “null@nospam. |
Error |
{0:Application |
Specifies the name to be used in the “From” field of the notification. You can use the Application |
Error |
The ‘{0:Exception |
Specifies the text to be used as the notification subject. You can use the Application |
Error |
True | Specifies if the SMTP connection is encrypted. The “True” and “1” values enable encryption, any other values – disable it. If omitted, the encryption is not used. This key value is ignored if the Error |
Simple |
simpleerrorreport. |
Specifies the URL of a custom web page that displays content as a simple error report. XAF displays a simple error report if an error occurs upon application startup, or if the rich error report content is not specified. Simple error report substitutes {REQUESTURL} and {DETAILS} strings with the request URL and error details respectively. Error details are visible if the request is local. If Simple |
Rich |
richerrorreport. |
Specifies the URL of a custom web page that displays content as an error report with rich text formatting. XAF displays this type of report if an error occurs at runtime. It substitutes {REQUESTURL} and {DETAILS} strings with the request URL and error details respectively. Error details are visible if the request is local. You can specify the “Error. |
Detailed |
192. |
Specifies the IP address of the remote host, which is allowed to get the detailed error information (including trace entries). If omitted, the error details are displayed for local requests only. |
These settings should be specified in the appSettings section of the Web.config file.
<appSettings>
<add key="ErrorReportEmail" value="admin@example.com" />
<add key="ErrorReportEmailFrom" value="application@example.com" />
<add key="ErrorReportEmailServer" value="smtp.example.com" />
<add key="SimpleErrorReportPage" value="" />
<add key="RichErrorReportPage" value="Error.aspx" />
<!-- ... -->
</appSettings>