tags:

views:

306

answers:

1

The situation I have is I am handling OnException in my controller. Depending on the type of exception I would like to return a javascript result. The action method that threw the exception was called by submitting an ajax form. The ajax form handles the success event. In my OnException method I am setting ExceptionHandled = true per all the examples I've seen. The problem is even though an exception was thrown and I returned a JavascriptResult the success method is called and the javascript I returned isn't executed. Any suggestions as to why this is happening and how I can get the JavascriptResult to execute.

+1  A: 

Here's the sequence of events:

  1. the browser sent an ajax request
  2. the action method on the server started executing, and threw an exception.
  3. your OnException method got a chance to look at the exception, and decided to set ExceptionHandled=true.
  4. further, your OnException method returned a JavascriptResult
  5. as ExceptionHandled was set to true, MVC stops the exception processing chain at that point, and returns the JavascriptResult to the browser, with an HTTP result code of 200 OK
  6. The ajax code on the browser receives the result, and interprets it as indicating all is well; it then calls OnComplete, followed by OnSuccess.

Your options are:

  1. Set a status code of 500 on the response before returning from OnException. The browser will call OnComplete, then OnFailure.

  2. Deal with the returned value in OnComplete, instead of in OnSuccess, and then return false to indicate OnSuccess should not be called.

Oren Trutner
I remembered about setting the status code right before you replied. It appears as if I have other problems though as my javascript result isn't executing. Thanks for your help.