1

I have this code in my global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    var exception = Server.GetLastError();
    var httpException = exception as HttpException;
    //...
}

If I call a URL like this, where NonExistingNonesense does not exist:

localhost/ExistingArea/ExistingController/NonExistingNonesense

Everything is fine. (The code from my global.asax will be called)

But if I call a url like this:

localhost/NonExistingNonesense

The code never reach the Application_Error method.

What could be the problem?


I have this in my web.config:

<system.web>
  <customErrors mode="On"></customErrors>
  <!-- ... -->
</system.web>
<system.webServer>
  <httpErrors existingResponse="PassThrough"/>
  <!-- ... -->
<system.webServer>

Please note:

I know it is possible with

<error statusCode="404" responseMode="ExecuteURL" path="/Error/PageNotFound" />

The problem is, I need to pass some exception context to my view, so I want to handle this by code.

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111

1 Answers1

0

Your ASP.NET code won't fire unless it is invoked by the IIS framework. By default, IIS will stop any request for a file that does not exist before it reaches your code. But you can turn this off.

Go into IIS admin and disable the Invoke handler only if request is mapped to a file option.

John Wu
  • 50,556
  • 8
  • 44
  • 80