RE: DefaultRedirect Problem

From: Steven Cheng[MSFT] (v-schang_at_online.microsoft.com)
Date: 02/03/04


Date: Tue, 03 Feb 2004 07:48:07 GMT

Hi Gary,

Thanks for posting in the community!
>From your description, you used a custom error page( a asp.net aspx page)
and set in the web.config via the
<customErrors defaultRedirect="CustomErrorPage.aspx" mode="On">
</customErrors>
so as to use the certain aspx page as the default error page. However, you
encoutered runtime error when execute and redirect to the error page, yes?
If there is anything I misunderstood, please feel free to let me know.

Based on my research, this problem is due to the Server's LastError has
already been cleared before the CustomError Page is loaded. For example, if
you add the below code in your custom error aspx page:

if(!IsPostBack)
{
        Exception exp = Server.GetLastError();
                                
        if(exp != null)
        {
                lblMessage.Text = exp.Message;
        }
}

we can find that the "exp" is always an empty object, so the server error
has been cleared up. So if we use Server.GetLastError().Message to retrieve
info from it, we'll get exception.
And One way to keep the server error information so as to be used in the
Custom error page is to pre-store the error's information before the custom
error page is loaded. For example, in the "Application_Error" event of the
Global class:
protected void Application_Error(Object sender, EventArgs e)
{
        Session["Last_Error_Info"] = Server.GetLastError().Message;
}

Then, in the custom error page, we can retrieve the info from the Session
so as to output it on the custom error page, such as:

public class CustomErrorPage : System.Web.UI.Page
{
                protected System.Web.UI.WebControls.Label lblMessage;
        
                private void Page_Load(object sender, System.EventArgs e)
                {
                        if(!IsPostBack)
                        {
                                string msg = (string)Session["Last_Error_Info"];
                                
                                if(msg != null)
                                {
                                        lblMessage.Text = msg;
                                }
                        }
                        
                }

.........................
}

Please check out my suggestion to see whether it helps you. If you feel
anything unclear or have any questions, please always feel free to let me
know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



Relevant Pages

  • Re: DefaultRedirect Problem
    ... you used a custom error page ... > so as to use the certain aspx page as the default error page. ... > info from it, we'll get exception. ... > protected void Application_Error(Object sender, ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Catch previous url
    ... "Jason Zhou" wrote: ... > I have a question on how to get the previous url in custom error file. ... > web site properties/Custom Errors. ... I replaced that 404b.htm with my aspx ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Catch previous url in custom error page
    ... If I don't use that custom error page, do I have anyway to get control even ... the file or directory specified in the request does not exist on my website? ... > web site properties/Custom Errors. ... I replaced that 404b.htm with my aspx ...
    (microsoft.public.dotnet.framework.aspnet)
  • Catch previous url
    ... I have a question on how to get the previous url in custom error file. ... If someone visit my website with a wrong file name or wrong directory name, ... web site properties/Custom Errors. ... I replaced that 404b.htm with my aspx ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Remote Errors return NULL
    ... Thanks for posting in the community! ... However, when the inner page(contained in the iframe) run into error, the ... error information didnt' display correctly. ... As for using a custom error page to display the application error. ...
    (microsoft.public.dotnet.framework.aspnet)