Shyju
Shyju

Reputation: 218822

ASP.NET Accessing cookie value in session_end event of global.asax

In ASP.NET , How can i retrieve a cookie value in the Session_End event of global.asax file ? The following code is throwing an exception "Object reference not set to an instance of an object"

    string cookyval = "";
    try
    {
        cookyval = Context.Request.Cookies["parentPageName"].Value;
    }
    catch (Exception ex)
    {
        cookyval = "";
    }

Any advice ?

Upvotes: 0

Views: 4526

Answers (3)

Paul Suart
Paul Suart

Reputation: 6713

The Session_End event is fired by the IIS worker process, not an HTTP request. Therefore your HttpContext will be null and you won't be able to set a client's cookie.

Upvotes: 3

Mark Brackett
Mark Brackett

Reputation: 85665

Session_End isn't run in the context of a user request, so there's no access to cookies (or any other request variables).

If you put the value into Session, I think you can access that:

string cookyval = "";
try
{
    cookyval = (string)Session["parentPageName"];
}
catch (Exception ex)
{
    cookyval = "";
} 

Otherwise, you'd need to write it to some other server side storage (like a database).

Upvotes: 0

Daniel Elliott
Daniel Elliott

Reputation: 22867

Not sure this is possible.

The request is no longer alive at the point the Session_End fires.

Sorry,

Dan

Upvotes: 0

Related Questions