views:

153

answers:

1

Hi folks,

i'm trying to send back a simple error message as Json, with the HTTP code as 404.

So i started out writing my own IExceptionFilter that checks to see the exception. To keep this simple, if the exception throw is of type ResourceNotFoundException then i set the code to 404. Otherwise everything else if 500.

Now, the problem is .. the default IIS7 404 error message is returned :( my code is called .. but it seems to bypass it (later on in the pipeline)...

is there some trick i need to do?

do I need a custom error handling (in the web config) to be turned on or something?


Edit: I'm trying to do what twitter does. Their Http Response Code documentation shows / explains some examples how they handle 404's, etc.. and i'm wanting to do that in my MVC app.

Edit 2: The code i've done is listed here, for anyones reference :)

A: 

When you are handling your exception, are you setting ExceptionHandled to true?

Here's a quick example...

HandleException(ActionExecutedContext filterContext)
{
  Exception exception = filterContext.Exception;

  //Check if our exception has been handled.
  if (filterContext.ExceptionHandled == false)
  {
    //Do your exception stuff
    filterContext.Result = YourExceptionMessageAsAnActionResult();
    //Set it as null.
    filterContext.ExceptionHandled = true;
    filterContext.HttpContext.Response.Clear();
  }
}
Dan Atkinson
Yes, I believe so. I've also added a link to the code i've done. Can u check that out, please?
Pure.Krome
I have replied to that message also. It looks like there is a small typo in your code, but I'm sure that this is just a typo in the question code, and not what's in your class.
Dan Atkinson
yeah, it was just a question typo as i reduced the real logic for berevity. *blush*.
Pure.Krome