Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Application hosted on IIS7 that is ignoring custom errors and falls back to IIS errors

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.

like image 931
Chris Marisic Avatar asked Jul 02 '10 14:07

Chris Marisic


People also ask

How do I hide detailed error information for IIS and ASP Net?

To prevent unauthorized users from viewing this privileged information, detailed error pages must not be seen by remote users. This setting can be modified in the errorMode attribute setting for a Web site's error pages. By default, the errorMode attribute is set in the Web.

How do I redirect an error page in web config?

The <customErrors> section in Web. config has two attributes that affect what error page is shown: defaultRedirect and mode . The defaultRedirect attribute is optional. If provided, it specifies the URL of the custom error page and indicates that the custom error page should be shown instead of the Runtime Error YSOD.


3 Answers

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.

like image 198
Michael A. Volz aka Flynn Avatar answered Oct 26 '22 19:10

Michael A. Volz aka Flynn


Instead of using:

Response.TrySkipIisCustomErrors = true;

Use this in Web.config:

<httpErrors errorMode="Custom" existingResponse="Replace">
like image 27
Lars B. Avatar answered Oct 26 '22 18:10

Lars B.


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.

like image 3
Andrew Chaa Avatar answered Oct 26 '22 20:10

Andrew Chaa