Skip to content

DevExpress-Examples/asp-net-mvc-handle-app-level-errors-occurred-during-callbacks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Use the static ASPxWebControl.CallbackError event to handle callback exceptions thrown by DevExpress MCV extensions server side. Delegate callback exception handling to the Application_Error event handler.

protected void Application_Start() {
    ASPxWebControl.CallbackError += Application_Error;
}
Protected Sub Application_Start()
	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.

protected void Application_Error(object sender, EventArgs e) {
    Exception exception = HttpContext.Current.Server.GetLastError();
    if (exception is HttpUnhandledException)
        exception = exception.InnerException;
    AddToLog(exception.Message, exception.StackTrace);
}
Protected Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim exception As Exception = HttpContext.Current.Server.GetLastError()
    If TypeOf exception Is HttpUnhandledException Then
        exception = exception.InnerException
    End If
    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.

<configuration>
    <devExpress>
        <errors callbackErrorRedirectUrl="/Home/Error" />
    </devExpress>
</configuration>

If the customErrors.mode option is set to On, add a custom error filter into FilterConfig to handle controller exceptions.

public class FilterConfig {
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
        filters.Add(new HandleErrorAttributeEx());
    }

    public class HandleErrorAttributeEx : HandleErrorAttribute {
        public HandleErrorAttributeEx() : base() { }
        public override void OnException(ExceptionContext filterContext) {
            base.OnException(filterContext);
            filterContext.ExceptionHandled = false;
        }
    }
}
Public Class FilterConfig
    Public Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
        filters.Add(New HandleErrorAttributeEx())
    End Sub

    Public Class HandleErrorAttributeEx
        Inherits HandleErrorAttribute

        Public Sub New()
            MyBase.New()
        End Sub
        Public Overrides Sub OnException(ByVal filterContext As ExceptionContext)
            MyBase.OnException(filterContext)
            filterContext.ExceptionHandled = False
        End Sub
    End Class
End Class

Files to Review

Documentation

More Examples