DefaultRedirect Problem

G

Gary

When using an ASPX page for a DefaultRedirect it does not work if the ASPX
page has any code in it.

If I put ...

If Not IsPostBack Then
lblMessage.Text = Server.GetLastError.Message
End If

in the ASPX page it does not display when an unhandled exception is thrown.
If I take the code out, the ASPX page displays properly when an unhandled
exception is thrown.

Does anyone know why this occurs?
Thanks,
G
 
A

Alvin Bruney [MVP]

I believe you need server.clearerror after getlast error call. != 100% sure
on this
 
S

Steven Cheng[MSFT]

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.)
 
S

Steven Cheng[MSFT]

Hi Gary,


After some further research, I found that the problem (can not retrieve the
error info via Server.GetLastError) was caused by the default behavior when
the ASP.NET runtime handling the unhandled exceptions. In fact, after the
"Application_Error" event, the ASP.NET runtime will clear the server error
and then direct user to the custom error page(if the setting is to use
custome error page), however, at that time ,since the server error is
cleared, so we are not able to retrieve it in the custom error page's
Page_Load event.

And in the last reply, I told you that we can store the error info in
Session and then retrieve from the Session again in Custom error page's
Page_Load. However, after testing this means, I found it also impossible
because after the Application_Error, the current Session will be stoped and
a new Session is to be started, so all the infos stored in Session also
losed. But I've found another way to retrieve the Server Error info in
custom error page's Page_Load event:
Just manually use "Server.Transfer" to forward the current response to the
Custom error page, that'll cause the error info still remain. For example,
here is the code I tested and succeeded:
event to custom error page 's Page_Load event:

##in Application_Error:
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError();

HttpContext.Current.Response.Clear();
Server.Transfer("CustomErrorPage.aspx");
}

##in CustomErrorPage.aspx 's code behind
public class CustomErrorPage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblCallStack;
protected System.Web.UI.WebControls.Label lblSource;
protected System.Web.UI.WebControls.Label lblMessage;

private void Page_Load(object sender, System.EventArgs e)
{
Exception ex = Server.GetLastError();
lblMessage.Text = ex.Message;
lblCallStack.Text = ex.StackTrace;
lblSource.Text = ex.Source;
}
....................
}

I manually forward the request to the custom erro page rather than let the
ASP.NET runtime do the default hehavior.

In addition, here is a tech article on providing custom error handing and
reporting:

#Rich Custom Error Handling with ASP.NET
http://msdn.microsoft.com/library/en-us/dnaspp/html/customerrors.asp?frame=t
rue

Please try out the above suggestions. If you feel anything unclear, please
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.)
 
G

Gary

Steven:

Thanks. Once I did the following in the application error event....
Dim unhandledException As Exception = _
Server.GetLastError().InnerException
Application("LastErrorMessage") = unhandledException.Message


Then it all worked fine. thanks for your help.

Gary
 
S

Steven Cheng[MSFT]

Hi Gary,


Thanks for your followup. I'm glad that you've figured out the means to
workaround this problem. However, since you used the Application State to
store the ServerError Info, and the server error may occcurs in every
requested thread, so there exist some potential concurrency issue on the
application state object. For example, multi user are visiting the site and
some of them encountered some serverside error. Then, all these thread will
try accessing the application's certain object. If you haven't explictly
provide concurrency protection on the application object, there will occur
concurrent issue. So do remember to explicitly provide synthronism
operations when accessing application object. For example, using the below
style code:
Application.Lock();

Application["error_info"] = Server.GetLastError().Message;

Application.UnLock();

And here is the description in MSDN on using the Application State
object(focus on concurrency issue):
----------------------------
The concurrency and synchronization implications of storing and accessing a
global variable within a multithreaded server environment. Multiple threads
within an application can access values stored in application state
simultaneously. You should always be careful to ensure that if an
application-scoped object is free-threaded, it contains built-in
synchronization support. All custom objects that target the common language
runtime are free-threaded. If an application-scoped object is not
free-threaded, you must ensure that explicit synchronization methods are
coded around it to avoid deadlocks, race conditions, and access violations.
----------------------------
For detailed info on using Application State, you may view the following
reference in MSDN:

#Application State
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconApplicationState.a
sp?frame=true

And here is a tech article on the concurrency issues of the Application
object in ASP, I also think it helpful to you since the situation is
similar in ASP.NET.

#Application Concurrency Issues
http://msdn.microsoft.com/library/en-us/dnproasp/html/applicationconcurrency
issues.asp?frame=true

In addtion, if you'd like to use other means to accomplish tranmiting the
server error info to custom error page, you may
have a further check on the suggestions in my former reply and if you have
any questions on it, please 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.)
 
G

Guest

Steven

Would you be able to convert this into VB for me, I understand what you are doing, I just don't know how to replicate it in V

----- Steven Cheng[MSFT] wrote: ----

Hi Gary


After some further research, I found that the problem (can not retrieve the
error info via Server.GetLastError) was caused by the default behavior when
the ASP.NET runtime handling the unhandled exceptions. In fact, after the
"Application_Error" event, the ASP.NET runtime will clear the server error
and then direct user to the custom error page(if the setting is to use
custome error page), however, at that time ,since the server error is
cleared, so we are not able to retrieve it in the custom error page's
Page_Load event

And in the last reply, I told you that we can store the error info in
Session and then retrieve from the Session again in Custom error page's
Page_Load. However, after testing this means, I found it also impossible
because after the Application_Error, the current Session will be stoped and
a new Session is to be started, so all the infos stored in Session also
losed. But I've found another way to retrieve the Server Error info in
custom error page's Page_Load event
Just manually use "Server.Transfer" to forward the current response to the
Custom error page, that'll cause the error info still remain. For example,
here is the code I tested and succeeded
event to custom error page 's Page_Load event

##in Application_Error
protected void Application_Error(Object sender, EventArgs e

Exception ex = Server.GetLastError()

HttpContext.Current.Response.Clear()
Server.Transfer("CustomErrorPage.aspx")


##in CustomErrorPage.aspx 's code behin
public class CustomErrorPage : System.Web.UI.Pag

protected System.Web.UI.WebControls.Label lblCallStack
protected System.Web.UI.WebControls.Label lblSource
protected System.Web.UI.WebControls.Label lblMessage

private void Page_Load(object sender, System.EventArgs e

Exception ex = Server.GetLastError();
lblMessage.Text = ex.Message
lblCallStack.Text = ex.StackTrace
lblSource.Text = ex.Source;

...................


I manually forward the request to the custom erro page rather than let the
ASP.NET runtime do the default hehavior

In addition, here is a tech article on providing custom error handing and
reporting

#Rich Custom Error Handling with ASP.NE
http://msdn.microsoft.com/library/en-us/dnaspp/html/customerrors.asp?frame=
ru

Please try out the above suggestions. If you feel anything unclear, please
feel free to let me know




Regards

Steven Chen
Microsoft Online Suppor

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

Guest

Steven

I'm still having problems with your example. I'll give you some more detail as to what I'm doing

In the Application_Start event in the global.axax I try and create an object of clsBackoffic

Dim BO as new clsBackoffic

I inentionally throw an exception back to the global.axa

I then follow your example in the Application_Err even
Dim ex As Exception = Server.GetLastErro
At this point I check to make sure that it is the exception that I have thrown before the server.transfer("errors.aspx"
HttpContext.Current.Response.Clear(
Server.Transfer("Errors.aspx"

Here's the proble

In the page_load event I put the code per your exampl

Dim ex As Exception = Server.GetLastErro
At this point I check the exception again at the command window and recieve this message
Referenced object has a value of 'Nothing'. So when you run the next line of code it will generate another exception when you try to set the text property of the label to the message property of the exception
lblErrMsg.Text = ex.Messag

Any clues

Thanks
Ken
 
S

Steven Cheng[MSFT]

Hi,

It's really strange since I put the same code as you in my web app(I tested
in both C# and VB.NETweb projects) and it worked well. Would you have a
try creating a new simple web project and test it again? This time, to make
it more simple, you may just put the following in the Application_Error:

Server.Transfer("Errors.aspx")

Also, don't forget the
<customErrors defaultRedirect="CustomErrorPage.aspx" mode="On" />
in the web.config file.


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.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top