views:

483

answers:

1

Howdy,

how do I throw a 404 or FileNotFound exception/result from my action and let IIS use my customErrors config section to show the 404 page?

I've defined my customErrors like so

<customErrors mode="On" defaultRedirect="/trouble">
  <error statusCode="404" redirect="/notfound" />
</customErrors>

My first attempt at an actionResult that tries to add this doesnt work.

public class NotFoundResult : ActionResult {
    public NotFoundResult() {

    }

    public override void ExecuteResult(ControllerContext context) {
        context.HttpContext.Response.TrySkipIisCustomErrors = false;
        context.HttpContext.Response.StatusCode = 404;
    }
}

But this just shows a blank page and not my /not-found page

:(

What should I do?

+6  A: 

This should do the trick:

throw new HttpException(404, "HTTP/1.1 404 Not Found");
Rob Levine