12
votes

I have a C# web forms ASP.NET 4.0 web application that uses Routing for URLs for some reason custom errors defined in the system.web section of my web.config is entirely ignored and it will fall back the IIS errors.

This gets entirely ignored

  <system.web>
    <customErrors mode="On">
      <error statusCode="500" redirect="~/Error" />
      <error statusCode="404" redirect="~/404" />
      <error statusCode="403" redirect="~/Error" />
    </customErrors>
  </system.web>

This part takes over

  <system.webServer>
    <httpErrors>
      <!--<clear />-->
      <remove statusCode="500" subStatusCode="-1" />
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" subStatusCode="-1" path="/App1/404" responseMode="Redirect" />
      <error statusCode="500" prefixLanguageFilePath="" path="/App1/Error" responseMode="Redirect" />
    </httpErrors>
  </system.webServer>

This would be a minor inconvenience except that by the fact it falls back to IIS native instead of my application it completely circumvents Elmah logging my 404 exceptions correctly.

Edit: Just to avoid any suggestions of such I only added the httpErrors configuration after customErrors stopped working so I would have anything.

3
I don't get this working as well. For 404 erros there seems to be a common workaround of using a "wildcard route" as proposed here: stackoverflow.com/questions/2704338/… . I've also noticed that the custom-404-page kicks in when you enter a wrong url which ends with .aspx. But if I omit the extension I am redirected to the default error page of IIS, like you decscribed. And I have absolutely no idea how to get custom error pages working for other errors than 404. I'm curious how your question will end up.Slauma
I'm glad to hear this has been viewed happening to others since I was pretty much at a loss until I realized how to configure httpErrors.Chris Marisic
Have you setup a route to ignore Error and 404?bleevo
This is discussed in the MVC overview tutorials. THe behaviour of IIS errors is dependent on your version of IIS. Using ASP.NET MVC with Different Versions of IIS (C#)Philip Smith
Don't think this is relevant, I'm using IIS 7+ in integrated mode.Chris Marisic

3 Answers

14
votes

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.

11
votes

Instead of using:

Response.TrySkipIisCustomErrors = true;

Use this in Web.config:

<httpErrors errorMode="Custom" existingResponse="Replace">
3
votes

You can pass through IIS7 default error messages in two ways One is to set response.TrySkipIisCustomErrors = true

response.TrySkipIisCustomErrors = true;
response.Status = response.Status;

For some reason, TrySkipIisCustomErrors is not honoured if you don't set response.Status.

The other is to set existingResponse to "PassThrough" in web.config

<configuration>
  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>
</configuration>

But this will ignore all set IIS custom error pages.