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
  • 15,685
  • 16
  • 74
  • 107

2 Answers2

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
  • 47,831
  • 8
  • 41
  • 73
0

I could not say why the code doesn't reach to Application_Error without debugging your code.

However, you might want to try filtering 404 status code from Exception, and redirect to custom 404 Page.

protected void Application_Error(object sender, EventArgs e)
{
    var exception = Server.GetLastError();

    // Process 404 HTTP errors
    var httpException = exception as HttpException;
    if (httpException != null && httpException.GetHttpCode() == 404)
    {
        Response.Clear();
        Server.ClearError();
        Response.TrySkipIisCustomErrors = true;

        // Call target Controller and pass the routeData.
        IController controller = new CommonController();

        var routeData = new RouteData();
        routeData.Values.Add("controller", "Common");
        routeData.Values.Add("action", "PageNotFound");

        var requestContext = new RequestContext(
             new HttpContextWrapper(Context), routeData);
        controller.Execute(requestContext);
    }
}

CommonController

public class CommonController : Controller
{
    [AllowAnonymous]
    public ActionResult PageNotFound()
    {
        Response.StatusCode = 404;
        Response.TrySkipIisCustomErrors = true;

        return View();
    }
}

Or

If you just want to retrieve exception context, you can retrieve it at Error action method.

  <system.web>
    <customErrors defaultRedirect="~/Common/Error" mode="On">
      <error statusCode="404" redirect="~/Common/PageNotFound"/>
    </customErrors>
  </system.web>


public class CommonController : Controller
{
    [AllowAnonymous]
    public ActionResult Error()
    {
        var exception = Server.GetLastError(); <--- Get Exception

        Response.StatusCode = 503;
        Response.TrySkipIisCustomErrors = true;

        return View();
    }   
}
Win
  • 58,817
  • 13
  • 98
  • 174