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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The IIS 7 behavior you're seeing is unique to IIS 7 and must be addressed specifically. Read the links in my answer.
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))
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.
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... we are 2! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.