0
votes

I have tried to set up a Custom 404 page, and it is redirecting to that URL. However, it's not showing the page it should be. IIS 6

So, I have the web.config setting AND I'm returning a 404 code on the NotFound.aspx page.

Web.config

<customErrors mode="RemoteOnly" defaultRedirect="~/error/Default.aspx">
  <error statusCode="404" redirect="~/error/NotFound.aspx" />
</customErrors>

NotFound.aspx.cs

public partial class error_NotFound : Custom.PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.StatusCode = 404; 
    }    
}

You can see here that this:

https://www.mshsaa.org/notapage.aspx

...redirects to this:

https://www.mshsaa.org/error/NotFound.aspx?aspxerrorpath=/notapage.aspx

NOTE This does appear to work correctly on my local machine, but not on live.

NOTE2 I see that there is also an IIS setting for a 404 Error that redirects.

So, what am I doing wrong?

1
Are you sure that the NotFound.aspx is both deployed and in the correct folder?Jonathon Chase
@JonathonChase It certainly appears to be there and in the correct folder.John Pasquet
Add redirectMode="ResponseRewrite" to customErrors.Alex Kudryashev
@AlexKudryashev That gives me a Runtime Error. And it doesn't even seem to redirect.John Pasquet

1 Answers

0
votes

This is working code from my production server.

web.config

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

404.aspx.cs

public partial class error_NotFound : Custom.PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        //do something with the exception
        Server.ClearError();
        Response.StatusCode = 404; 
    }    
}