.NET MVC 3 Custom AJAX Error Handling

Custom Message From AJAX Error Handling with jQuery and MVC 3
Custom Message From AJAX Error Handling with jQuery and MVC 3

An exception on the server-side will invoke the JavaScript AJAX error handling function. This blog will detail two methods to provide custom error handling, overriting the default.

Both methods allow specific error messages to be returned to the client error handling code. This example applies to .NET c# MVC 3 with jQuery and jQuery form plugin. Before quickly scanning this blog for the solution, please note that the second solution detailed below appears to be a better option.

Example 1

Index.cshtml

 

HomeController.cs

The [HandleError(…] attribute is the critical part of all this. It specifies NullReferenceException as the Exception type to watch for. If that Type of Exception occurs within this controller method, the View ‘AjaxError’ will be rendered. Note that there is no Controller method for ‘AjaxError’. If one is included, it is not invoked.

Uncomment the line that throws the exception to demonstrate the error handling.

[HandleError…] can be declared on the class (Controller Level) also, this will mean that it applies to all methods within the class/Controller.

AjaxError.cshtml

 Note here that the HandleErrorInfo object is passed to the error page. This allows details of the Exception to be rendered.

 

Web.config needs a single change to make this work:

customError mode must be set to ‘On’. If this is not set, this method of error handling will not work. (mode=”remoteOnly” will cause the ErrorHandler to be invoked only when ‘localhost’ is not the domain, this facilitates debugging on localhost as the Stack Trace can be available on localhost but automatically hidden on subsequent servers)

And that’s pretty much it……….

Custom Message From AJAX Error Handling with jQuery and MVC 3

Custom Message From AJAX Error Handling with jQuery and MVC 3

However,

the Web.config configuration may not be desirable so here is an alternative and perhaps better method of achieving the same.

Example 2

Create a filter class called ‘AjaxErrorHandlerAttribute’

AjaxErrorHanderAttribute.cs

 Note that this code checks for an AJAX request. If true, it prints out the error messages. If the request is not an AJAX request, this code has no effect and the error is handled in the normal way.

Now, the only thing left is to modify the attribute in the controller:

 Note that the attribute here is ‘AjaxErrorHandlerAttribute’ that we just created.

Note. In Web.config, the attribute <customErrors mode=”On” /> is no longer necessary.

Now, there is one possible pitfall…..

If [HandleError] is at the class/Controller level, it’s automatically applied to each method, i.e.

On localhost, when the attribute [AjaxErrorHandler] is applied to a method everything works as it should, i.e. the method level handler is invoked and the class level handler is not.

However, on any server other than localhost the class level [ErrorHandler] may be invoked (see some info <customErrors> above, this may affect non-localhost environments).

To prevent it from being invoked and only have the method level handler invoked, add Order = 2 to [ErrorHandler]

I.e.

Problem solved, now the method level handler is invoked and the class level handler is not.

The reason why is because the class or Controller level handler is now lower priority than the method level one. Therefore the method level one is invoked first. Because the method level one flags the error as handled the next handler is not invoked.

For more on how this is the case and handler priority, see this MSDN page, navigate to the ‘Order’ section.

This pretty much it.

All comments welcome.