468,133 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

ASP.NET 1.1 HttpContext.Session question

Hi,
I have a web application, code of ASPX page needs to call one static
method in another class which does not inherit System.UI.Page class

using System.Web;

public class Utils {
public static void GetSessions() {
HttpContext ctx = HttpContext.Current;
ctx.Session; // not available. <undefined value>
ctx.Cache; // Yes!! I can access.
}
}

If I place a quick watch on ctx variable, I noticed Session is not
availble to use, but Cache is ready.

Anybody has idea how can I access Session objects from other class? And
why I cannot access it via HttpContext?

Thanks for any suggestion.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
Jan 25 '06 #1
7 4048
put in a page1.aspx

Do NOTHING on this page, except put a button on it, redirecting to another
page.
like this:
Response.Redirect("page2.aspx");
ok.. now debug your stuff. (aka, don't call Utils.GetSessions() until you're
on page2.aspx) (but you *must* redirect from page1.aspx)

I think the Session will be available on the second page.

...


"Hardy Wang" <ha*******@newsgroups.nospam> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
Hi,
I have a web application, code of ASPX page needs to call one static
method in another class which does not inherit System.UI.Page class

using System.Web;

public class Utils {
public static void GetSessions() {
HttpContext ctx = HttpContext.Current;
ctx.Session; // not available. <undefined value>
ctx.Cache; // Yes!! I can access.
}
}

If I place a quick watch on ctx variable, I noticed Session is not
availble to use, but Cache is ready.

Anybody has idea how can I access Session objects from other class? And why I cannot access it via HttpContext?

Thanks for any suggestion.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy

Jan 25 '06 #2
Thanks,
But I have to put this piece of code in a separate class out of web
page.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
"sloan" <sl***@ipass.net> wrote in message
news:ei*************@TK2MSFTNGP10.phx.gbl...
put in a page1.aspx

Do NOTHING on this page, except put a button on it, redirecting to another
page.
like this:
Response.Redirect("page2.aspx");
ok.. now debug your stuff. (aka, don't call Utils.GetSessions() until
you're
on page2.aspx) (but you *must* redirect from page1.aspx)

I think the Session will be available on the second page.

..


"Hardy Wang" <ha*******@newsgroups.nospam> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
Hi,
I have a web application, code of ASPX page needs to call one static
method in another class which does not inherit System.UI.Page class

using System.Web;

public class Utils {
public static void GetSessions() {
HttpContext ctx = HttpContext.Current;
ctx.Session; // not available. <undefined value>
ctx.Cache; // Yes!! I can access.
}
}

If I place a quick watch on ctx variable, I noticed Session is not
availble to use, but Cache is ready.

Anybody has idea how can I access Session objects from other class?

And
why I cannot access it via HttpContext?

Thanks for any suggestion.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy


Jan 25 '06 #3

That's fine, you can do what you want.
What I am saying is that the Session may not be available UNTIL AFTER (at
least) one aspx page loads.
That has nothing to do with where you write/store the code.
I'm trying to show you when Session becomes "alive", not where to put your
code.
...

"Hardy Wang" <ha*******@newsgroups.nospam> wrote in message
news:#$*************@tk2msftngp13.phx.gbl...
Thanks,
But I have to put this piece of code in a separate class out of web
page.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
"sloan" <sl***@ipass.net> wrote in message
news:ei*************@TK2MSFTNGP10.phx.gbl...
put in a page1.aspx

Do NOTHING on this page, except put a button on it, redirecting to another page.
like this:
Response.Redirect("page2.aspx");
ok.. now debug your stuff. (aka, don't call Utils.GetSessions() until
you're
on page2.aspx) (but you *must* redirect from page1.aspx)

I think the Session will be available on the second page.

..


"Hardy Wang" <ha*******@newsgroups.nospam> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
Hi,
I have a web application, code of ASPX page needs to call one static method in another class which does not inherit System.UI.Page class

using System.Web;

public class Utils {
public static void GetSessions() {
HttpContext ctx = HttpContext.Current;
ctx.Session; // not available. <undefined value>
ctx.Cache; // Yes!! I can access.
}
}

If I place a quick watch on ctx variable, I noticed Session is not
availble to use, but Cache is ready.

Anybody has idea how can I access Session objects from other class?

And
why I cannot access it via HttpContext?

Thanks for any suggestion.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy



Jan 25 '06 #4
Hardy ,

That basically means that the HttpHandler that process the request is not
marked with one of the marker interfaces IRequiresSessionState or I
ReadOnlySessionState. In this case Session won't be created, but other think
like Cache for example will be avavilable.

This can happen if you have written your own HttpHandler or the page has
been set up not to have session state.
For the latter check if the @ Page directive in aspx file doesn't have
EnableSessionState=false. Also check if the session state is not disabled in
the web.config file - sessionState element's mode attribute doesn't have to
be *Off*.
--
HTH
Stoitcho Goutsev (100)
"Hardy Wang" <ha*******@newsgroups.nospam> wrote in message
news:uS**************@TK2MSFTNGP09.phx.gbl...
Hi,
I have a web application, code of ASPX page needs to call one static
method in another class which does not inherit System.UI.Page class

using System.Web;

public class Utils {
public static void GetSessions() {
HttpContext ctx = HttpContext.Current;
ctx.Session; // not available. <undefined value>
ctx.Cache; // Yes!! I can access.
}
}

If I place a quick watch on ctx variable, I noticed Session is not
availble to use, but Cache is ready.

Anybody has idea how can I access Session objects from other class? And
why I cannot access it via HttpContext?

Thanks for any suggestion.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy

Jan 25 '06 #5
sloan,

As I tried to explain in my other post creation of the session doesn't
depend on whether aspx page has been loaded or not. The SessionStateModule
initializes the context with session if the HttpHandle that is about to
process the request is marked with proper marker interface. The HttpHandler
could asp.net page, but might be custom written HttpModule.

Further more two pages in one application could be marked one to support
session state and the other not to.
--

Stoitcho Goutsev (100)
"sloan" <sl***@ipass.net> wrote in message
news:OT*************@TK2MSFTNGP12.phx.gbl...

That's fine, you can do what you want.
What I am saying is that the Session may not be available UNTIL AFTER (at
least) one aspx page loads.
That has nothing to do with where you write/store the code.
I'm trying to show you when Session becomes "alive", not where to put your
code.
..

"Hardy Wang" <ha*******@newsgroups.nospam> wrote in message
news:#$*************@tk2msftngp13.phx.gbl...
Thanks,
But I have to put this piece of code in a separate class out of web
page.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
"sloan" <sl***@ipass.net> wrote in message
news:ei*************@TK2MSFTNGP10.phx.gbl...
> put in a page1.aspx
>
> Do NOTHING on this page, except put a button on it, redirecting to another > page.
> like this:
> Response.Redirect("page2.aspx");
>
>
> ok.. now debug your stuff. (aka, don't call Utils.GetSessions() until
> you're
> on page2.aspx) (but you *must* redirect from page1.aspx)
>
> I think the Session will be available on the second page.
>
> ..
>
>
>
>
> "Hardy Wang" <ha*******@newsgroups.nospam> wrote in message
> news:uS**************@TK2MSFTNGP09.phx.gbl...
>> Hi,
>> I have a web application, code of ASPX page needs to call one static >> method in another class which does not inherit System.UI.Page class
>>
>> using System.Web;
>>
>> public class Utils {
>> public static void GetSessions() {
>> HttpContext ctx = HttpContext.Current;
>> ctx.Session; // not available. <undefined value>
>> ctx.Cache; // Yes!! I can access.
>> }
>> }
>>
>> If I place a quick watch on ctx variable, I noticed Session is not
>> availble to use, but Cache is ready.
>>
>> Anybody has idea how can I access Session objects from other
>> class?
> And
>> why I cannot access it via HttpContext?
>>
>> Thanks for any suggestion.
>>
>> --
>> WWW: http://hardywang.1accesshost.com
>> ICQ: 3359839
>> yours Hardy
>>
>>
>
>



Jan 25 '06 #6
Use HttpContext.Current in your class.
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Hardy Wang" wrote:
Hi,
I have a web application, code of ASPX page needs to call one static
method in another class which does not inherit System.UI.Page class

using System.Web;

public class Utils {
public static void GetSessions() {
HttpContext ctx = HttpContext.Current;
ctx.Session; // not available. <undefined value>
ctx.Cache; // Yes!! I can access.
}
}

If I place a quick watch on ctx variable, I noticed Session is not
availble to use, but Cache is ready.

Anybody has idea how can I access Session objects from other class? And
why I cannot access it via HttpContext?

Thanks for any suggestion.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy

Jan 26 '06 #7
Pardon me. I should have read more carefully. I see that you aready tried
that.
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Dale" wrote:
Use HttpContext.Current in your class.
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Hardy Wang" wrote:
Hi,
I have a web application, code of ASPX page needs to call one static
method in another class which does not inherit System.UI.Page class

using System.Web;

public class Utils {
public static void GetSessions() {
HttpContext ctx = HttpContext.Current;
ctx.Session; // not available. <undefined value>
ctx.Cache; // Yes!! I can access.
}
}

If I place a quick watch on ctx variable, I noticed Session is not
availble to use, but Cache is ready.

Anybody has idea how can I access Session objects from other class? And
why I cannot access it via HttpContext?

Thanks for any suggestion.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy

Jan 26 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Francois Malgreve | last post: by
4 posts views Thread by Makarand Keer | last post: by
15 posts views Thread by Jason | last post: by
7 posts views Thread by Hardy Wang | last post: by
6 posts views Thread by Sjaakie | last post: by
2 posts views Thread by Dave | last post: by
3 posts views Thread by Madhur | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.