Last post Sep 17, 2009 09:54 AM by Augi
Sep 16, 2009 07:26 AM|pure.krome|LINK
i've got some simple Actions that return a JsonAction. nothing tough there. But, if there's a problem (eg. some exceptions are thrown) i wish to throw return certain HTTP status codes, like a 404 (resource not found), 500 (my code is bad and sucks), 401
(not authorised), etc. I got this idea from Twitter,
here. It's not a new thing, but I sorta like it :)
So, i tried making my own IExceptionFilter, called HandleErrorAsJson. That's not too hard, I thought :) I then have that checking for certain (custom) exceptions. Based upon the (custom) exception, it sets the status code. otherwise, it's unhandled
and therefor the status code is 500. I'm also trying to send back a json messge, just like twitter...
Result => HTTP/1.1 404 Not Found
But when i do this, my IIS7 site returns the default ASP.NET 404 error html :(
Any ideas? here's the code i have...
public class HandleErrorAsJson : FilterAttribute, IExceptionFilter
#region IExceptionFilter Members
public void OnException(ExceptionContext filterContext)
var controller = filterContext.Controller as Controller;
if (controller == null ||
Exception exception = filterContext.Exception;
if (exception == null)
// Action method exceptions will be wrapped in a
// TargetInvocationException since they're invoked using
// reflection, so we have to unwrap it.
if (exception is TargetInvocationException)
exception = exception.InnerException;
StringCollection messages = new StringCollection();
// NOTE :: Only one exception is listed here, for berevity.
if (exception is ResourceNotFoundException)
statusCode = 404;
messages.Add("The resource you were looking for cannot be found.");
statusCode = 500;
// Json serialize the messages.
// TODO: make this return the request and then the error.
filterContext.Result = new JsonResult
Data = messages.Count > 0 ? messages : null
// Prepare the response code.
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.StatusCode = statusCode;
And is this an ok way of handling errors when doing a json (or xml) request?
Sep 17, 2009 02:36 AM|pure.krome|LINK
* Polite bump *
Sep 17, 2009 08:45 AM|DanAtkinson|LINK
Although I'm sure that it's an error in writing out the code, line 36 should be adding to messages, rather than message.
Sep 17, 2009 09:02 AM|Augi|LINK
Be sure that you have properly configured customErrors section in your
Sep 17, 2009 09:18 AM|pure.krome|LINK
Could you please elaborate on this? i must admit, I did have a look at this and was stuck on what to provide ... because i don't want a custom PAGE to redirect/display .. i've already defined/written the output i want.
Example (web.config) code please? pretty please?
@Dan : cheers :) that was just some pseduo code error while i was trying to cut away the real code *blush* to make this question lighter :) Now fixed :P
Sep 17, 2009 09:54 AM|Augi|LINK
Here is description of
customErrors element. I think that you should turn off custom errors. If this will not work then you should change error pages configuration in IIS.