5

I'm running my MVC 3 app (recently updated from 2) on IIS 7.5 (Win 7 64bit) with a .NET 4.0 integrated pipeline app pool and have the following set-up in web.config:

<customErrors mode="On" defaultRedirect="~/Problem/Oops" redirectMode="ResponseRedirect">
    <error statusCode="404" redirect="~/Problem/NotFound" />
</customErrors>

If an action method on a controller throws an exception the server and hence generates a 500 errorcode it correctly sends the browser to the default redirect URL.

However if my action deliberately returns a HttpNotFoundResult via HttpNotFound() I get the IIS 7.5 404.0 error page and not the one indicated in my web.config.

If I enter a URL that doesn't exist on my app like http://localhost/MyApp/FOO then I do get shown the page as indicated by the web.config.

Anybody have any ideas why I'm not getting redirected to my custom 404 error page when using HttpNotFound()?

2 Answers 2

6

Please try below syntax instead of calling HttpNotFound and let me know the result ;)

throw new HttpException(404, "NotFound");
0
0

Have you tried setting Response.TrySkipIisCustomErrors = true;?

(see http://blog.janjonas.net/2011-04-13/asp_net-prevent-iis_75_overriding-custom-error-page-iis-default-error-page)

4
  • Ooh sounds promising. I will give it a try.
    – Peter
    Apr 14, 2011 at 8:26
  • Unfortunately this didn't work for me. The code in my ProblemsController never gets touched. IIS seems to be ignoring the settings or I'm misinterpreting the use of the MVC3 HttpNotFound() method.
    – Peter
    Apr 14, 2011 at 9:08
  • Accepting this answer as it was the most helpful and might work for others even if it didn't work for me.
    – Peter
    Apr 20, 2012 at 10:52
  • This didn't work for me. Matin Habibi's suggestion below did. Aug 14, 2015 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.