views:

1503

answers:

1

Suppose I have a method in my controller that is called via a jQuery AJAX call. E.g. I'd like to delete a user. When everything goes fine, I return new Content('ok') and exit the method.

What should I do when an error occured? I'd like to indicate it by an appropriate status code, so that my error call back would be called called. Why status code? Read here: http://stackoverflow.com/questions/407651/how-do-you-trigger-the-error-callback-in-a-jquery-ajax-call-using-asp-net-mvc However, the approach doesn't work because IIS7 returns it's own message (Bad request) insted of my custom error message.

Besides that there are two other catches:

  1. It has to work with IIS6 as well
  2. IE8 doesn't return the 'Bad request' string. Inside error callback the property request.responseTest is null.

The error callback could look like this: error: function(request) { alert(request.responseText);}

+4  A: 

Setting the Response.StatusCode is the correct thing to do. To fix IIS's "helpful" error handling, set the HttpResponse.TrySkipIisCustomErrors property. You can read more about this here.

Craig Stuntz
HttpResponse.TrySkipIisCustomErrors doesn't work with IIS6
stej
The IIS 7 behavior you're seeing is unique to IIS 7 and must be addressed specifically. Read the links in my answer.
Craig Stuntz
Probably I don't understand. The links provided are only about IIS 7, but I need a solution for IIS 6 as well (as pointed in (1))
stej
IIS 6 doesn't replace 500 errors in the way that IIS 7 does. But the MVC framework will if you throw an exception and "catch" it with [HandleError]. Those are two entirely separate problems. Similarly for any custom error handling you define in Web.config. Your post describes a real problem with IIS 7, and I gave you the fix for that. You *may* also be experiencing an entirely separate problem with similar symptoms that also shows up in IIS 6, but (1) there *will not* be one solution which fixes both problems, and (2) there isn't enough info in your question to diagnose the second issue.
Craig Stuntz
If I could vote for this 10x, I would. I just went through 1.5 days of pain trying to figure this out. Thanks!!!
Mark Struzinski