2

In my global.asax file, I have the following code:

void Application_Error(object sender, EventArgs e)
{
    Exception TheError = Server.GetLastError();

    if (TheError is HttpException && ((HttpException)TheError).GetHttpCode() == 404)
    {
        Response.Redirect("~/404.aspx");
    }
    else
    {
        Response.Redirect("~/500.aspx");
    }
}

When I navigate to an non-existing page, I get the generic error page. I don't want anything pertaining to custom error in the web.config because my plan is to add code to the global.asax file to log the exceptions. Is there a way to handle custom error with just the global.asax file?

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
frenchie
  • 51,731
  • 109
  • 304
  • 510

2 Answers2

5

EDIT:

The answer is not obvious, but this seems to explain it: http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs

If you scroll down a ways, you'll find this lovely tidbit:

Note: The custom error page is only displayed when a request is made to a resource handled by the ASP.NET engine. As we discussed in the Core Differences Between IIS and the ASP.NET Development Server tutorial , the web server may handle certain requests itself. By default, the IIS web server processes requests for static content like images and HTML files without invoking the ASP.NET engine. Consequently, if the user requests a non-existent image file they will get back IIS's default 404 error message rather than ASP.NET's configured error page.

I've emphasized the first line. I tested this out in the default template, and sure enough, this URL:

http://localhost:49320/fkljflkfjelk

gets you the default IIS page, whereas simply appending a .aspx makes the Application_Error kick in. So, it sounds like you need to enable customErrors/httpErrors if you want to have all errors handled.

For IIS <= 6, add to <system.web>:

<customErrors mode="On" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="/404.aspx"/>
  <error statusCode="500" redirect="/500.aspx"/>
</customErrors>

For IIS7+, add to <system.webServer>:

<httpErrors errorMode="Custom">
  <remove statusCode="404"/>
  <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL"/>
  <remove statusCode="500"/>
  <error statusCode="500" path="/500.aspx" responseMode="ExecuteURL"/>
</httpErrors>

I'll leave the original answer in case someone finds it useful.


I believe you need to clear the existing error code from the response, otherwise IIS ignores your redirect in favor of handling the error. There's also a Boolean flag, TrySkipIisCustomErrors, you can set for newer versions of IIS (7+).

So, something like this:

void Application_Error(object sender, EventArgs e)
{
    Exception TheError = Server.GetLastError();
    Server.ClearError();

    // Avoid IIS7 getting in the middle
    Response.TrySkipIisCustomErrors = true;

    if (TheError is HttpException && ((HttpException)TheError).GetHttpCode() == 404)
    {
        Response.Redirect("~/404.aspx");
    }
    else
    {
        Response.Redirect("~/500.aspx");
    }
}
Community
  • 1
  • 1
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • I put a breakpoint on line 1, go to a non-existent page but still getting the generic error page. – frenchie Aug 15 '14 at 06:11
  • By "generic error page", do you mean Error.aspx (or whatever the WebForms equivalent is), or the browser's generic error page? – Tieson T. Aug 15 '14 at 06:14
  • By generic I mean the default asp error page; see edit. – frenchie Aug 15 '14 at 06:22
  • Ok, saw the edit: I added in in the web.config but it's still not working. I do see the breakpoint and the custom error page when adding .aspx, which is what your answer describes. Still working on it too. – frenchie Aug 15 '14 at 06:58
  • @frenchie Adding the `httpErrors` section seems to enable error handling in the test project I started; have to remember that IIS7 and up primarily use the `system.webServer` section. Tweak the redirect mode if you prefer redirection rather than rewriting. – Tieson T. Aug 15 '14 at 07:21
1

Looks like IIS is looking for a page and did not find it.

Make sure:

  1. 404.aspx is created
  2. customErrors tag from web.config is not present.
Nikhil Agarwal
  • 138
  • 1
  • 7