Development web server fires Application_Error on 404, why doesn't IIS7?

1k Views Asked by At

I'm using Application_Error to catch some legacy URLs and URL shortcuts. In Global.vb I have this code:

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim serverError = TryCast(Server.GetLastError(), HttpException)
        If serverError IsNot Nothing Then
            Dim errorCode As Integer = serverError.GetHttpCode()
            If 404 = errorCode Then
               ' Do some custom processing here
            End If
        End If
    End Sub

In web.config I have this, to ensure that all requests, not just ones ending in .aspx, are handled by aspnet_isapi.dll so I get to process them:

        <add name="ASP.NET-ISAPI-2.0-Wildcard" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="classicMode,runtimeVersionv2.0,bitness32" />

On my development box (using Cassini), this works fine in all cases: Both /badurl and /badurl.aspx cause Application_Error to fire.

In IIS7, however, /badurl.aspx works as expected, but /badurl just results in a generic server-generated 404 page.

Any ideas what causes the difference, and how I can get IIS7 to replicate the development server's behavior?

2

There are 2 best solutions below

2
On

You can try in two ways:

  1. in your code you can set Response.TrySkipIisCustomErrors = true;
  2. in config file you can set:

<customErrors redirectMode="ResponseRewrite mode="On" defaultRedirect="appError.aspx" />

1
On

try to add this to web.config file.

 <customErrors mode="On" defaultRedirect="appError.aspx">
          <error statusCode="403" redirect="appError.aspx"/>
          <error statusCode="404" redirect="appError.aspx"/>
        </customErrors>