13

I have an ASP.NET MVC 2 application, which has an Application_Error event handler in global.asax. In this, I'm detecting the case where the Exception type is HttpException and the HTTP code is 404, and redirecting to my own 404-handling page.

This works fine on my Cassini development server, but now I'm trying to move it over to my production server which has IIS7 (using integrated mode).

When I request a non-existent URL, IIS7 is showing its own 404 page, and so far as I can tell, my Application_Error method is never called.

How do I fix that?

Gary McGill
  • 26,400
  • 25
  • 118
  • 202
  • Clear the error using `Server.ClearError()` before attempting the redirect. – devstuff Jul 20 '10 at 15:47
  • Similar [question](http://stackoverflow.com/questions/1171035/asp-net-mvc-custom-error-handling-application-error-global-asax) – nubm Apr 13 '10 at 17:51
  • Similar, but the OP's code and the code in the accepted answer is pretty close to what I have. Which works on Cassini (and possibly IIS in classic mode), but doesn't seem to work - at least for me - in IIS7 integrated mode. – Gary McGill Apr 13 '10 at 20:08

5 Answers5

6

I answered that in another post: ASP.NET Application hosted on IIS7 that is ignoring custom errors and falls back to IIS errors

"To disable the IIS error messages you have to set

  Response.TrySkipIisCustomErrors = true;

in your error page. After that, your Error messages should show without problem."

Community
  • 1
  • 1
  • I've not tried it, since I no longer work with this website, but this looks convincing and seems to work for others, so... accepted. – Gary McGill Jul 11 '12 at 23:28
  • For some reason, this approach doesn't work with 404.15 status code. IIS handles this kind of errors by itself in spite of setting Response.TrySkipIisCustomErrors = true. – Serg Jul 14 '17 at 15:21
5

Can't you just turn off the 404 httpError:

    <httpErrors errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
    </httpErrors>
Koen Rijpstra
  • 344
  • 2
  • 8
0

The Answer by Flynn solved my issue of ASP.NET taking control of the 404

Now I am able to do this:

Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 404;
Response.Write("<script type='text/javascript'>setTimeout(function () { window.location = '/'; }, 1000)</script>");
HttpContext.Current.ApplicationInstance.CompleteRequest();
Registered User
  • 3,669
  • 11
  • 41
  • 65
0

In ASP.NET MVC I use a custom ErrorController with an Index method (default handler) and a few custom handlers (401, 404) using the following web.config settings

<!--  CUSTOM ERROR MESSAGES
Set customErrors mode="On" or "RemoteOnly" to enable custom error messages, "Off" to disable. 
Add <error> tags for each of the errors you want to handle.   
"On" Always display custom (friendly) messages.
"Off" Always display detailed ASP.NET error information.
"RemoteOnly" Display custom (friendly) messages only to users not running 
on the local Web server. This setting is recommended for security purposes, so 
that you do not display application detail information to remote clients. -->
<customErrors mode="Off" defaultRedirect="~/Error">
    <error statusCode="401" redirect="~/Error/Unauthorized" />
    <error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>

I don't use Application_Error except for logging purposes.

Todd Smith
  • 17,084
  • 11
  • 59
  • 78
  • 1
    @Todd: since I have all my error handling logic written and working locally, I really don't want to have to switch to another method. I'm hoping that someone can tell me the magic incantation to ward off the evil spirits of IIS7. Also, I too use Application_Error to do some logging, and as I mentioned in my question it's not actually getting there - presumably because of the same issue - so I don't get any logging either :-( – Gary McGill Apr 13 '10 at 20:07
  • @Todd: I also read somewhere that the customErrors block is *not* used in IIS7 integrated mode. – Gary McGill Apr 14 '10 at 08:26
0

This is discussed in the MVC overview tutorials. It is probably the the case that your IIS 7 is set up in Classic mode. See:

Using ASP.NET MVC with Different Versions of IIS (C#)

Philip Smith
  • 2,741
  • 25
  • 32
  • I specifically said in the question that I'm using integrated mode. I even put it in bold. :-) – Gary McGill Jul 19 '10 at 13:22
  • My appoligies, I missed that. Must be my age. In that case you need to set the HttpResponse StatusCode to a non error value. In this way IIS will not intercept your redirect. The Error page should return 404 not the redirect to it. – Philip Smith Jul 19 '10 at 13:30