Example E2398
Visible to All Users

ASP.NET Web Forms - How to handle application-level errors occurred during callbacks

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 Basic
Sub 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 Basic
Sub 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 the ASPxWebControl.CallbackError event. Use the default Application_Error event handler for this purpose.

Files to Review

Documentation

More Examples

Example Code

CallbackErrorEvent/Global.asax
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>
CallbackErrorEvent/Web.config
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>

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.