Use the static ASPxWebControl.CallbackError event to handle callback exceptions thrown by DevExpress web controls server side. Delegate callback exception handling to the Application_Error
event handler.
C#void Application_Start(object sender, EventArgs e) {
// Assign Application_Error as a callback error handler
ASPxWebControl.CallbackError += new EventHandler(Application_Error);
}
Visual BasicSub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Assign Application_Error as a callback error handler
AddHandler ASPxWebControl.CallbackError, AddressOf Application_Error
End Sub
The Application_Error
event handler catches all unhandled ASP.NET errors while processing a request. You can use the GetLastError method to get and log the details of the last exception.
C#void Application_Error(object sender, EventArgs e) {
// Use HttpContext.Current to get a Web request processing helper
Exception exception = HttpContext.Current.Server.GetLastError();
if (exception is HttpUnhandledException)
exception = exception.InnerException;
// Log an exception
AddToLog(exception.Message, exception.StackTrace);
}
Visual BasicSub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Use HttpContext.Current to get a Web request processing helper
Dim exception As Exception = HttpContext.Current.Server.GetLastError()
If TypeOf exception Is HttpUnhandledException Then
exception = exception.InnerException
End If
' Log an exception
AddToLog(exception.Message, exception.StackTrace)
End Sub
When a callback exception occurs, you can redirect the application to another web resource. Use the callbackErrorRedirectUrl configuration option to specify the redirection location.
XML<configuration>
<devExpress>
<errors callbackErrorRedirectUrl="~/ErrorPage.aspx"/>
</devExpress>
</configuration>
Note
There are controls (for instance, ASPxUploadControl) that utilize the capabilities of the DevExpress.Web.ASPxUploadProgressHttpHandler handler to perform actions on a callback. System-level exceptions (request timeout, session timeout, etc.) that occur while executing the
ASPxUploadProgressHttpHandler
handler cannot be handled using theASPxWebControl.CallbackError
event. Use the defaultApplication_Error
event handler for this purpose.
Files to Review
- Global.asax (VB: Global.asax)
- Web.config (VB: Web.config)
Documentation
More Examples
Example Code
Code<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="DevExpress.Web" %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e) {
// Assign Application_Error as a callback error handler
ASPxWebControl.CallbackError += new EventHandler(Application_Error);
}
void Application_Error(object sender, EventArgs e) {
// Use HttpContext.Current to get a Web request processing helper
HttpServerUtility server = HttpContext.Current.Server;
Exception exception = server.GetLastError();
if (exception is HttpUnhandledException)
exception = exception.InnerException;
// Log an exception
AddToLog(exception.Message, exception.StackTrace);
}
void AddToLog(string message, string stackTrace) {
StringBuilder sb = new StringBuilder();
sb.AppendLine(DateTime.Now.ToLocalTime().ToString());
sb.AppendLine(message);
sb.AppendLine();
sb.AppendLine("Source File: " + HttpContext.Current.Request.RawUrl);
sb.AppendLine();
sb.AppendLine("Stack Trace: ");
sb.AppendLine(stackTrace);
for (int i = 0; i < 150; i++)
sb.Append("-");
sb.AppendLine();
HttpContext.Current.Session["Log"] += sb.ToString();
sb.AppendLine();
}
</script>
Code<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="devExpress">
<section name="themes" type="DevExpress.Web.ThemesConfigurationSection, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
<section name="compression" type="DevExpress.Web.CompressionConfigurationSection, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
<section name="settings" type="DevExpress.Web.SettingsConfigurationSection, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
<section name="errors" type="DevExpress.Web.ErrorsConfigurationSection, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="DevExpress.Data.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=B88D1754D700E49A" />
<add assembly="DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=B88D1754D700E49A" />
<add assembly="DevExpress.Printing.v15.1.Core, Version=15.1.15.0, Culture=neutral, PublicKeyToken=B88D1754D700E49A" />
<add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="DevExpress.RichEdit.v15.1.Core, Version=15.1.15.0, Culture=neutral, PublicKeyToken=B88D1754D700E49A" />
</assemblies>
</compilation>
<pages validateRequest="false"></pages>
<customErrors redirectMode="ResponseRedirect" defaultRedirect="~/ErrorPage.aspx" mode="On">
<error statusCode="500" redirect="~/ErrorPage.aspx" />
</customErrors>
<httpModules>
<add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule" />
</httpModules>
<httpHandlers>
<add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" verb="GET,POST" path="DX.ashx" validate="false" />
<add verb="GET,POST" path="ASPxUploadProgressHandlerPage.ashx" type="DevExpress.Web.ASPxUploadProgressHttpHandler, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
</httpHandlers>
</system.web>
<system.webServer>
<modules>
<add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add type="DevExpress.Web.ASPxHttpHandlerModule, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" verb="GET,POST" path="DX.ashx" name="ASPxHttpHandlerModule" preCondition="integratedMode" />
<add name="ASPxUploadProgressHandler" preCondition="integratedMode" verb="GET,POST" path="ASPxUploadProgressHandlerPage.ashx" type="DevExpress.Web.ASPxUploadProgressHttpHandler, DevExpress.Web.v15.1, Version=15.1.15.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
</handlers>
</system.webServer>
<devExpress>
<themes enableThemesAssembly="true" styleSheetTheme="" theme="" customThemeAssemblies="" />
<compression enableHtmlCompression="false" enableCallbackCompression="true" enableResourceCompression="true" enableResourceMerging="true" />
<settings doctypeMode="Xhtml" rightToLeft="false" embedRequiredClientLibraries="false" ieCompatibilityVersion="edge" />
<errors callbackErrorRedirectUrl="~/ErrorPage.aspx" />
</devExpress>
</configuration>