473,465 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Application_Error() cannot access Session if the error is HTTP 404

4 New Member
I have the following code in my global.asax file:
Expand|Select|Wrap|Line Numbers
  1. void Application_Error(object sender, EventArgs e) 
  2. {
  3.      string ex = Server.GetLastError().ToString();
  4.      HttpContext.Current.Session["HttpContext_ERROR"] = HttpContext.Current;
  5.      HttpContext.Current.Session["LastError"] = ex;
  6.      // this just logs various info about the error
  7.      ErrorLogger.LogError("Uncaught exception", ex, HttpContext.Current);
  8.      Server.ClearError();
  9. }
This works just fine for HTTP 500 (Server Error) errors, but if it's a HTTP 404 (File Not Found) error, the Session is always null. Why? Is there any way to read or write to the Session in Application_Error for a 404?
Dec 6 '07 #1
1 6362
Frinavale
9,735 Recognized Expert Moderator Expert
I have the following code in my global.asax file:
Expand|Select|Wrap|Line Numbers
  1. void Application_Error(object sender, EventArgs e) 
  2. {
  3.      string ex = Server.GetLastError().ToString();
  4.      HttpContext.Current.Session["HttpContext_ERROR"] = HttpContext.Current;
  5.      HttpContext.Current.Session["LastError"] = ex;
  6.      // this just logs various info about the error
  7.      ErrorLogger.LogError("Uncaught exception", ex, HttpContext.Current);
  8.      Server.ClearError();
  9. }
This works just fine for HTTP 500 (Server Error) errors, but if it's a HTTP 404 (File Not Found) error, the Session is always null. Why? Is there any way to read or write to the Session in Application_Error for a 404?
The HttpContext object is created by ASP.NET during the page request. Since no page was found, no HttpContext Object can be created; therefore, no Session can exist when a 404 error occurs.

-Frinny
Dec 6 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Greg Burns | last post by:
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,...
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...
0
by: DalePres | last post by:
I have an error handling page that is called by Global.ASAX in the Application_Error handler. When I test Session.Count in Application_Error, it has a count of 2 and I can access the two session...
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...
2
by: Daflookie | last post by:
For one reason or another I am unable to access Session contents in my asp.net application via the global.asax's Application_Error event. I can pull this exact code snippet below out of...
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: jobs | last post by:
I'm forcing an error/exception in one page as follows Protected Overrides Sub OnLoad(ByVal e As EventArgs) Dim badthing As New Exception("XXX") Throw badthing End Sub And attempting to set...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.