I was trying to return an error to the call to the controller as advised in
This link so that client can take appropriate action.
The controller is called by javascript via jquery AJAX. I am getting the Json object back only if I don’t set the status to error.
Here is the sample code
if (response.errors.Length > 0)
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(response);
I get the Json if I don’t set the statuscode.
If I set the status code I get the status code back but not the Json error object.
Update
I want to send an Error object as JSON so that it can be handled error callback of ajax.
I found the solution here
I had to create a action filter to override the default behaviour of MVC
Here is my exception class
Note that I have constructor which initializes my JSON. Here is the action filter
Now that I have the action filter, I will decorate my controller with the filter attribute
When the error is thrown the action filter which implements IExceptionFilter get called and I get back the Json on the client on error callback.