When ASP.NET MVC throws an exception, it returns a 500 error with response type text/html- which, of course, is invalid JSON.
I want to respond to an Ajax request expecting JSON with an error I can receive and display to the user.
Is it possible to return JSON with an HTTP status code of 500?
When the problem is a missing parameter, the 500 error occurs before the controller is even called - so a controller solution might not work. For example, leaving a required parameter out in a call to an Action that normally returns a JsonResult, ASP.NET MVC sends this back to the client:
Server Error in '/' Application. The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult EditUser(Int32, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String)' in 'bhh'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
I'm using jQuery; is there a better way to handle this?