'How Do I Disable IIS Error Pages And Handle Errors In Global.asax?

I've been trying to disable IIS error handling so I can capture all errors in Global.asax.

I am running Windows 2012 with IIS version 6.2.

For example, when I navigate to a missing image file such as:

www.mysite.com/thisimagesdoesntexist.jpg

I get the IIS error page and my Global.asax does not capture the error.

I tried adding to my web.config:

<system.webServer>
<httpErrors existingResponse="Passthrough" />
</system.webServer>

And also, in Global.asax:

Server.ClearError()
Response.TrySkipIisCustomErrors = True

However, the missing image error still is handled by IIS. Here is the code of my web.config and my Global.asax routine:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <!--Unobtrusive validation used JQuery-->
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
  </appSettings>
  <system.web>
    <httpRuntime targetFramework="4.6.1" />
    <!--Enable Sessions and don't scroll a form when doing a postback-->
    <!-- clientIDMode="Static" ensures the ASP.net controls have the exact name specified in their ID tags
         This is important so CSS targets the correct IDs for styling. -->
    <pages enableSessionState="true" controlRenderingCompatibilityVersion="3.5" clientIDMode="Static" maintainScrollPositionOnPostBack="true" />
    <!--Use cookies if enabled; use querystring if disabled-->
    <!--<sessionState mode="InProc" cookieless="AutoDetect" timeout="20" />-->
    <!-- cookieless="AutoDetect" is adding a cookie parameter to the URL that was causing FastSpring posts to fail -->
    <sessionState mode="InProc" timeout="20" />
    <!--Show detailed errors on the website while developing-->
    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.6.1" />
    <!--Use Tracing-->
    <trace enabled="true" pageOutput="false" requestLimit="40" localOnly="false" />
    <globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="auto" uiCulture="auto" />
  </system.web>
  <!--
  <system.webServer>
    <httpErrors errorMode="Detailed" />
  </system.webServer>
  -->
  <system.webServer>
    <httpErrors existingResponse="Passthrough" />
  </system.webServer>
  <system.codedom>
    <compilers>
      <compiler extension=".cs" language="c#;cs;csharp" warningLevel="4" compilerOptions="/langversion:7.0 /nowarn:1659;1699;1701;612;618" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      <compiler extension=".vb" language="vb;vbs;visualbasic;vbscript" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </compilers>
  </system.codedom>
</configuration>
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

  Dim Uri As Uri = New Uri(Request.Url.AbsoluteUri)

  Dim ex As HttpException = CType(Server.GetLastError(), HttpException)
  Dim httpCode As Integer = ex.GetHttpCode()
  Dim strErrorDescription As String = ex.Message.ToString

  Server.ClearError()
  Response.TrySkipIisCustomErrors = True

  If httpCode = 500 Then
    Response.Redirect("~/error.aspx?type=globalerror&errdescription=" & HttpUtility.UrlEncode(strErrorDescription) & "&url=" & HttpUtility.UrlEncode(Uri.ToString))
  ElseIf httpCode = 404 Then
    Response.Redirect("~/404.aspx?type=globalhttp&errdescription=" & HttpUtility.UrlEncode(strErrorDescription) & "&url=" & HttpUtility.UrlEncode(Uri.ToString))
  Else
    'Let's see what other errors are picked up and logged. Perhaps we log these to a separate table?
    Response.Redirect("~/error.aspx?type=globalhttp&errdescription=" & HttpUtility.UrlEncode(strErrorDescription) & "&url=" & HttpUtility.UrlEncode(Uri.ToString))
  End If

End Sub


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source