How to prevent "aspxerrorpath" being passed as a query string to ASP.NET custom error pages

If you supply your own query string variable when specifying the path, then .NET will NOT tack on the "aspxerrorpath". Who knew?

For example:

<customErrors mode="On" defaultRedirect="errorpage.aspx?error=1" >

This will do the trick. I had to add this to a bunch of apps since URLScan for IIS by default rejects anything with "aspxerrorpath" in it anyway.


In the global.asax, catch the 404 error and redirect to the file not found page. I didn't require the aspxerrorpath and it worked a treat for me.

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
    if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
    {
        Response.Redirect("~/filenotfound.aspx");
    }
    else
    {
        // your global error handling here!
    }
}

You could just send your own url params to the error page

<customErrors mode="On" defaultRedirect="~/default.html?404"> 
               <error statusCode="404" redirect="~/PageNotFound.html?404" /> 
</customErrors>