Peter
Peter

Reputation: 844

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

Upvotes: 5

Views: 1566

Answers (2)

Matin Habibi
Matin Habibi

Reputation: 700

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

throw new HttpException(404, "NotFound");

Upvotes: 6

janjonas
janjonas

Reputation: 2593

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)

Upvotes: 0

Related Questions