26

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: 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);}

Community
  • 1
  • 1
stej
  • 28,745
  • 11
  • 71
  • 104

1 Answers1

30

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
  • 125,891
  • 12
  • 252
  • 273
  • 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 May 20 '09 at 12:31
  • 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 May 21 '09 at 06:09
  • 1
    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 May 21 '09 at 12:34
  • 2
    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 Jul 14 '10 at 16:59
  • Mark Struzinski... we are 2! =) – Adriano Galesso Alves Aug 19 '14 at 20:00