Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS 7.5 not taking notice of customErrors for 404 returned by MVC 3 app

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()?

like image 596
Peter Avatar asked Feb 24 '11 13:02

Peter


1 Answers

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

throw new HttpException(404, "NotFound");
like image 186
Matin Habibi Avatar answered Sep 27 '22 16:09

Matin Habibi