Tom Gullen
Tom Gullen

Reputation: 61773

Web.config custom errors not working for 404's

<customErrors mode="On" defaultRedirect="~/Error.aspx">
    <error statusCode="500" redirect="~/500/"/>
    <error statusCode="404" redirect="~/404/"/>
</customErrors>

This works fine for requests like:

~/egerg.aspx
~/wf92734eg.aspx

But for any other extension, this gives a server 404 page! How can I make the server 404 page redirect to my error page?

Upvotes: 1

Views: 2126

Answers (1)

This solution works on my website which is 99% webforms and 1% Razor/MVC 4 on .NET Framework 4.

  1. In /Web.config:

    <system.web>
      <customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx">
        <error statusCode="500" redirect="~/500/"/>
        <error statusCode="404" redirect="~/404/"/>
      </customErrors>
    </system.web>
    
  2. In the Page_Load event of your 404 handler (/404/default.aspx.cs, I assume):

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.StatusCode = 404;
        Response.TrySkipIisCustomErrors = true;
    }
    
  3. Repeat the last step for the 500 handler.

  4. The Application_Error method is empty in /Global.asax.cs.

This solution is very similar to this one.

Upvotes: 1

Related Questions