0
votes

I configured my custom errors in global.asax like this :

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

        if (exc.GetType() == typeof(HttpException))
        {
            var http_ex = exc as HttpException;

            var error_code = http_ex.GetHttpCode();

            if (error_code == 404)
            {
                Response.Redirect("~/404.aspx", true);
            }
            if (error_code == 500)
            {
                Response.Redirect("~/500.aspx", true);
            }
        }
    }

It was working fine.

Later in my development, I had to use Ajax Page Method. For this, I added the ScriptModule handler in the web.config :

<modules>
    <remove name="ScriptModule" />
    <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>

But now, the 404 errors are falling back to the ugly IIS default 404 page.

I added a breakpoint in the global.asax, at this line :

Response.Redirect("~/404.aspx", true);

and the breakpoint is well triggered. When I hit F10 on this line, it instantly redirect to the ugly IIS 404 page. (The error is for the requested page, not for the 404.aspx page).

The web server is IIS Express from Visual Studio 2013. I have no other handler or module in my web.config. I'm running Asp.Net Web Forms, Framework 3.5.

Could you please explain why, and/or give me solution for this strange behavior ?

1

1 Answers

0
votes

I finally found the answer from Thomas Marquardt's Blog post here: http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx

I added Server.ClearError(), here is my Application_Error now:

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

    if (exc.GetType() == typeof(HttpException))
    {
        Server.ClearError();

        var http_ex = exc as HttpException;

        var error_code = http_ex.GetHttpCode();

        if (error_code == 404)
        {
            Response.Redirect("~/404.aspx", true);
        }
        if (error_code == 500)
        {
            Response.Redirect("~/500.aspx", true);
        }
    }
}