Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Handling in ASP.NET MVC and Ajax - [HandleException] filter

All,

I'm learning MVC and using it for a business app (MVC 1.0).

I'm really struggling to get my head around exception handling. I've spent a lot of time on the web but not found anything along the lines of what I'm after.

We currently use a filter attribute that implements IExceptionFilter. We decorate a base controller class with this so all server side exceptions are nicely routed to an exception page that displays the error and performs logging.

I've started to use AJAX calls that return JSON data but when the server side implementation throws an error, the filter is fired but the page does not redirect to the Error page - it just stays on the page that called the AJAX method.

Is there any way to force the redirect on the server (e.g. a ASP.NET Server.Transfer or redirect?)

I've read that I must return a JSON object (wrapping the .NET Exception) and then redirect on the client, but then I can't guarantee the client will redirect... but then (although I'm probably doing something wrong) the server attempts to redirect but then gets an unauthorised exception (the base controller is secured but the Exception controller is not as it does not inherit from this)

Has anybody please got a simple example (.NET and jQuery code). I feel like I'm randomly trying things in the hope it will work

Exception Filter so far...

public class HandleExceptionAttribute : FilterAttribute, IExceptionFilter
{
    #region IExceptionFilter Members

    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.ExceptionHandled)
        {
            return;
        }

        filterContext.Controller.TempData[CommonLookup.ExceptionObject] = filterContext.Exception;

        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            filterContext.Result = AjaxException(filterContext.Exception.Message, filterContext);
        }
        else
        {
            //Redirect to global handler
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = AvailableControllers.Exception, action = AvailableActions.HandleException }));
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
        }
    }

    #endregion

    private JsonResult AjaxException(string message, ExceptionContext filterContext)
    {
        if (string.IsNullOrEmpty(message))
        {
            message = "Server error";   //TODO: Replace with better message
        }

        filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;       //Needed for IIS7.0

        return new JsonResult
        {
            Data = new { ErrorMessage = message },
            ContentEncoding = Encoding.UTF8,
        };
    }
}
like image 876
GrahamB Avatar asked Apr 03 '10 20:04

GrahamB


People also ask

What are the exception filters in MVC?

Exception filter in MVC provides an ability to handle the exceptions for all the controller methods at a single location. This is by creating a class, which inherits from the FilterAttribute and IExceptionFilter interface.


1 Answers

I use the OnFailure hanlder in Ajax.Beginform(). The client-side failure handler can redirect by setting window.location (among a number of other options.) This will work in 99% of modern browsers- if the browser supports AJAX it should support this.

like image 150
Dave Swersky Avatar answered Nov 15 '22 13:11

Dave Swersky