473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DefaultRedirect Problem

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.GetLastE rror.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
Nov 18 '05 #1
21 2692
I believe you need server.clearerr or after getlast error call. != 100% sure
on this

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Gary" <gb@nospam.co m> wrote in message
news:OZ******** ******@TK2MSFTN GP09.phx.gbl...
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.GetLastE rror.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

Nov 18 '05 #2
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 ="CustomErrorPa ge.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.GetLastE rror();

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.GetLastE rror().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_Er ror" event of the
Global class:
protected void Application_Err or(Object sender, EventArgs e)
{
Session["Last_Error_Inf o"] = Server.GetLastE rror().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.P age
{
protected System.Web.UI.W ebControls.Labe l lblMessage;

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!IsPostBack)
{
string msg = (string)Session["Last_Error_Inf o"];

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

Nov 18 '05 #3
Hi Gary,
After some further research, I found that the problem (can not retrieve the
error info via Server.GetLastE rror) was caused by the default behavior when
the ASP.NET runtime handling the unhandled exceptions. In fact, after the
"Application_Er ror" 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_Err or, 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.Transfe r" 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_Err or:
protected void Application_Err or(Object sender, EventArgs e)
{
Exception ex = Server.GetLastE rror();

HttpContext.Cur rent.Response.C lear();
Server.Transfer ("CustomErrorPa ge.aspx");
}

##in CustomErrorPage .aspx 's code behind
public class CustomErrorPage : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Labe l lblCallStack;
protected System.Web.UI.W ebControls.Labe l lblSource;
protected System.Web.UI.W ebControls.Labe l lblMessage;

private void Page_Load(objec t sender, System.EventArg s e)
{
Exception ex = Server.GetLastE rror();
lblMessage.Text = ex.Message;
lblCallStack.Te xt = 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...rs.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.)
Nov 18 '05 #4
Steven:

Thanks. Once I did the following in the application error event....
Dim unhandledExcept ion As Exception = _
Server.GetLastE rror().InnerExc eption
Application("La stErrorMessage" ) = unhandledExcept ion.Message
Then it all worked fine. thanks for your help.

Gary
"Steven Cheng[MSFT]" <v-******@online.m icrosoft.com> wrote in message
news:qz******** ******@cpmsftng xa07.phx.gbl...
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 ="CustomErrorPa ge.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.GetLastE rror();

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.GetLastE rror().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_Er ror" event of the
Global class:
protected void Application_Err or(Object sender, EventArgs e)
{
Session["Last_Error_Inf o"] = Server.GetLastE rror().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.P age
{
protected System.Web.UI.W ebControls.Labe l lblMessage;

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!IsPostBack)
{
string msg = (string)Session["Last_Error_Inf o"];

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

Nov 18 '05 #5
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.Loc k();

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

Application.UnL ock();

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...icationState.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...ionconcurrency
issues.asp?fram e=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.)
Nov 18 '05 #6
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.GetLastE rror) was caused by the default behavior when
the ASP.NET runtime handling the unhandled exceptions. In fact, after the
"Application_Er ror" 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_Err or, 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.Transfe r" 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_Err or
protected void Application_Err or(Object sender, EventArgs e

Exception ex = Server.GetLastE rror()

HttpContext.Cur rent.Response.C lear()
Server.Transfer ("CustomErrorPa ge.aspx")
##in CustomErrorPage .aspx 's code behin
public class CustomErrorPage : System.Web.UI.P ag

protected System.Web.UI.W ebControls.Labe l lblCallStack
protected System.Web.UI.W ebControls.Labe l lblSource
protected System.Web.UI.W ebControls.Labe l lblMessage

private void Page_Load(objec t sender, System.EventArg s e

Exception ex = Server.GetLastE rror();
lblMessage.Text = ex.Message
lblCallStack.Te xt = 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...ors.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.

Nov 18 '05 #7
Hi,

Here is a webpage which can help convert C# code into VB.NET:

http://authors.aspalliance.com/aldot...translate.aspx

Hope this helps. Thanks.

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

Nov 18 '05 #8
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_Sta rt 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.GetLastE rro
At this point I check to make sure that it is the exception that I have thrown before the server.transfer ("errors.asp x"
HttpContext.Cur rent.Response.C lear(
Server.Transfer ("Errors.asp x"

Here's the proble

In the page_load event I put the code per your exampl

Dim ex As Exception = Server.GetLastE rro
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

Nov 18 '05 #9
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_Err or:

Server.Transfer ("Errors.asp x")

Also, don't forget the
<customErrors defaultRedirect ="CustomErrorPa ge.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

Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5206
by: gilberto ramirez | last post by:
I want to create an ASP application in the following way: PC1 - Windows XP (SP1) with VS .NET (and everything needed) - im programming with C# .NET SRV1 - ISA SERVER - Windows 2000 (SP3) SRV2 - in a virtual server called xxAP.xxx.xxx.com it has: - Windows 2000 (SP3) - FPSE ver 2002 (correctly configured)
6
4123
by: Gary | last post by:
I have the following in my web.config.... <customErrors mode="RemoteOnly" defaultRedirect="Applicationerror.aspx"/> but, my error page does not display. Instead a 'Runtime Error' page displays showing how to setup CustomErrors. However, if I make my error page a .htm page it shows fine. Anyone know why?
11
1441
by: James Warren | last post by:
I just installed VB.net 2003, upgrading from VB6. I tried to open a new ASP.net Web Application and immediately got an error stating that my Web Server isn't running ASP.net version 1.1. I tried running aspnet_regiis /i and all the dotnetfix "framework repair" suggestions the Help link sent me to, but no luck. I suspect I am not the first...
1
1403
by: Nathan Sokalski | last post by:
I have retrieved data from a database using a SELECT statement that includes an INNER JOIN. The data seems to be retrieved to the DataSet OK, but I am having trouble getting the data from the DataSet. The code I am using is as follows: Private Sub btnDownloadDB_Click(ByVal sender As System.Object, ByVal e As...
0
955
by: raja | last post by:
Hi, I want to upload one file from network,which it was not working but when i try to upload the same file from server system it is working. Is there any problems with network settings? I also checked the network settings like IUSR,IWAM,Administrator,asp.net and i allotted all the permissions to it and morover we were using workgroup.
0
1080
by: aj123 | last post by:
Hello; I develop website and uploaded to host provider and works fine but I uploaded it to ther hosting provider and it not working and give this problem ----- Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being...
4
6005
by: =?Utf-8?B?TWFyaw==?= | last post by:
I think I am missing something about how to redirect to a certain page when 404 errors occur. I want to redirect to a generic pagenotfound.aspx page. I have the following in web.config: <customErrors mode="RemoteOnly" defaultRedirect="/misc/pagenotfound.aspx"> </customErrors> However, when I enter a non-existent URL I just get the default...
6
1844
by: khanpmd | last post by:
Hi , I have a doubt in ASP.Net hosting.I have developed a site and hosted it. when i browse that site an error occured like this.I dont know why this error is showing? Because i have no problem when i test in localhost.The error is as follows Server Error in '/' Application. Runtime Error Description: An application error occurred on...
6
3243
by: praveenb000 | last post by:
we hosted a website http://vijayawadanalanda.org developed using asp.net and back end MS Accees database. There is some database connectivity problem occurring when saving the information in page url : http://vijayawadanalanda.org/feedback.aspx getting error: Not a valid file name. Please provide me solution to rectify this problem.
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.