Session Start / Application_BeginRequest Issues

G

Guest

I'm trying to write a log file that captures the referring url if the request is a new session, and captures a querystring value if the user is browsing between pages on the website.

When using the code below, I get the following error...

"System.NullReferenceException: Object reference not set to an instance of an object"

******************************************
protected void Session_Start(Object sender, EventArgs e)
{
System.Uri refURL = Context.Request.UrlReferrer;
if(refURL != null)
{
Context.Session.Add("referingURL", refURL.ToString());
}
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string refPage = Context.Request.QueryString.Get("Page");
if(refPage != null)
{
Context.Session.Add("referingPage", refPage.ToString());
}
}
******************************************

Does anyone know what I'm doing wrong?

Thanks in advance for your help!
 
C

Craig Deelsnyder

I'm trying to write a log file that captures the referring url if the
request is a new session, and captures a querystring value if the user
is browsing between pages on the website.

When using the code below, I get the following error...

"System.NullReferenceException: Object reference not set to an instance
of an object"

******************************************
protected void Session_Start(Object sender, EventArgs e)
{
System.Uri refURL = Context.Request.UrlReferrer;
if(refURL != null)
{
Context.Session.Add("referingURL", refURL.ToString());
}
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string refPage = Context.Request.QueryString.Get("Page");
if(refPage != null)
{
Context.Session.Add("referingPage", refPage.ToString());
}
}
******************************************

Does anyone know what I'm doing wrong?

Thanks in advance for your help!

Which one is erroring out? Application_BeginRequest is the first 'event'
in the chain, it's so early that Session is not available in it, so that
will get you in trouble there.

Also, I believe in Session_Start the Context still doesn't have a
reference to the Session, so unfortunately you're out of luck there as
well.

Others have said you can access it in Application_PreRequestHandlerExecute
and Application_AcquireRequestState
 
G

Guest

It errors out on Application_BeginRequest.

Are you suggesting that I move the code from Session_Start to Application_AcquireRequestState and from Application_BeginRequest to Application_PreRequestHandlerExecute?

Also.. where can I find a complete listing of all of the events that occur during the lifecycle of a web application including Application_PreRequestHandlerExecute
and Application_AcquireRequestState?

Thank you for your help.
 
C

Craig Deelsnyder

It errors out on Application_BeginRequest.

Are you suggesting that I move the code from Session_Start to
Application_AcquireRequestState and from Application_BeginRequest to
Application_PreRequestHandlerExecute?

Also.. where can I find a complete listing of all of the events that
occur during the lifecycle of a web application including
Application_PreRequestHandlerExecute
and Application_AcquireRequestState?

Thank you for your help.

Some more info:

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607#2

Actually, I think my statement before was incorrect; been awhile since I
looked at this myself, but in Session you should be able to see the
Session object, makes sense, see the example here:

http://samples.gotdotnet.com/quickstart/aspplus/doc/stateoverview.aspx

Sorry for the confusion...
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top