8

Good day!

I use strategy to handle 404 errors like this: Error handling for ASP.NET MVC 2 and IIS 7.0 or this: How can I properly handle 404 in ASP.NET MVC?

In short: I handle 404 as exception in Global.asax without adding any routing rules, if the exception is 404 I render special controller\action with error message.

On IIS6 it works with ASP.NET wildcard mapping. On IIS7 in integrated mode I need to add the following to the Web.config (where /error/HttpError404 is my action with 404 page):

    <httpErrors>
        <remove statusCode="403" subStatusCode="-1" />
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/error/HttpError404" responseMode="ExecuteURL" />
        <error statusCode="403" prefixLanguageFilePath="" path="/error/HttpError403" responseMode="ExecuteURL" />
    </httpErrors>

Why? Routing works differently on IIS integrated mode and IIS6 wildcard mapping?

Thanks in advance!

UPDATE: According to my tests it seems that my error handling works and my 404 action is rendered, but it seems that IIS sees 404 response code (which I set programmatically in my 404 action) and replace my page with default errors.

When I set <httpErrors> I got two hits for 404 page: one from exception handling in Global.asax and one from IIS.

Can this be the cause?

Community
  • 1
  • 1
artvolk
  • 9,448
  • 11
  • 56
  • 85

1 Answers1

11

It seems I have fixed it by setting in Web.config:

<httpErrors existingResponse="PassThrough" />
artvolk
  • 9,448
  • 11
  • 56
  • 85
  • I've had the same problem for months and this worked for me. Thanks for the update! – Nathan Birkes Jan 13 '11 at 16:29
  • 2
    I know it's a bit late, but what has worked with me with MVC3 is using exisitingResponse="Replace" – Jamie R Rytlewski Oct 05 '11 at 18:02
  • 1
    Setting exisitingResponse="Replace" in system.webserver seems to be incompatible with in system.web. With both set, I get a 500 page without detail removed where previously I would see my custom error page. I ended up using this solution instead -http://stackoverflow.com/a/9026941/678453 – Paul George Jul 12 '12 at 12:43
  • You little beauty :) Thanks. Everyone, be sure to put this inside – VictorySaber Jun 08 '15 at 14:53