473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Application_Error, Session, and custom error page

I am trying to write a global custom error page. I thought I would jot down
some of what I've learned so far...

At first I just used the default customErrors section with a defaultRedirect
tag, as such:

<customErrors defaultRedirect="error.aspx" mode="On" />

First thing I found out about this method, is that I didn't have access to
the exception object in my error.aspx page. So this page was going to be
very uninformitive. It appears that the Server.GetLastError cannot be
called outside of the Application_Error event in the global.asax file.

I found this asp.netPRO example that I am now using as a starting point:
http://www.aspnetpro.com/features/20...200305dk_f.asp

In the article the author saves the Exception in a "global" module variable
(how strange) so that he could use it later in his custom error page. This
worked Ok, but this doesn't appear to be thread-safe. So I quickly decided
I should be using a Session variable instead. The author hints that there
are other methods of doing this, but wisely avoids the whole issue. :)

Lots of examples on Google of people trying to stuff their Exception into a
Session("LastError") in the Application_Error event. Just as many people
wondering why it isn't working. ;(

Turns out you can write to the Session object in the Application_Error
event, but it won't save it unless you also do a Server.ClearError. Ouch!
Take a look http://tinyurl.com/r3xk for a quick explanation.

So now my Application_Error event is looking like this:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

'Grab a reference to the last error. Use InnerException because
'ASP.NET wraps errors here in an HttpUnhandledException.

Session("LastError") = Server.GetLastError.InnerException

Server.ClearError() ' must clear error else session state will not
be preserved!!!

' since manually clearing error, must manually redirect...
'Server.Transfer("~/error.aspx")
Response.Redirect("~/error.aspx")

End Sub

I have also taken out the defaultRedirect="error.aspx" tag in the web.config
since this will make it pointless(?)

Notice, I abandoned the server.transfer call in favor on the
response.redirect. Every single time I try and use server.transfer the page
will ignore my style sheet(!) Anybody?

Since my app is also using forms authentication, I went back an made sure my
error page was accesible whether logged in or not...

<location path="Error.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>

I have seen people using this syntax in their web config: <location
path="~/Error.aspx">, but this doesn't appear to work (or even be
necessary).

Anyways, I hope this helps somebody out there.

Greg


Nov 17 '05 #1
1 5876
Me
Because the error page is in a different directory from
the page where the error occurred. When Server.Transfer
is called, the processing switches to the new page (error
page in this case), but the URL in the browser is still
the initial page where the error occured. So the error
page is effectively being displayed in a different
directory than it's actual location.... causing it not to
be able to find the CSS.

-----Original Message-----

Notice, I abandoned the server.transfer call in favor on
the
response.redirect. Every single time I try and use
server.transfer the page
will ignore my style sheet(!) Anybody?

Nov 17 '05 #2

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

Similar topics

3
by: Mike Malter | last post by:
I have a general error page that I configured in web.config as <customErrors mode="On" defaultRedirect="CRDefaultError.aspx" /> This error page comes up whenever an error occurs outside of any...
3
by: MattC | last post by:
When a System.ArgumentOutOfRangeException is throw it gets caught in my Application_Error event. But I want it to be dealt with and then passed to a specific page. I dont want to use redirect...
4
by: Michael | last post by:
Using some example code I've found, I'm trying to do the custom error page thing. I have the following in global.asax.. Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) Dim...
6
by: Matt | last post by:
Can anyone give me a good reason to use BOTH application scope Page_Error and the page scope Page_Error when trapping errors in a web application? Is there any real benefit to using the Page_Error...
1
by: robertlair | last post by:
I am having issues with a production server with the GetLastError() method. Here is what I am doing: Application_Error on Global ASAX: Note: I am doing it this way (with ClearError) because...
8
by: tshad | last post by:
I have an Application_Error function in my Global.asax function that works fine until I try to access my Session variables. I am emailing the results to myself whenever I get an error and would...
10
by: Nemisis | last post by:
Hi everyone, I am trying to create a custom error page, that the user gets shown when a error has occurred within my website. The code works perfectly, apart from when an invalid URL is typed...
8
by: bmukulu | last post by:
Hi, I am trying to add some error handling in a Global.asax file. I am declaring a session variable within the Application_Error procedure. However, everytime i try to pass something into the...
1
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
I have a web app that handles errors in the Gloabal.ascx's Application_Error(). It does some logging etc in there and if it throws an error, I want the app to display a static html splash page...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.