ASP.NET 1.1 HttpContext.Session question

H

Hardy Wang

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.
 
S

sloan

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.

...
 
H

Hardy Wang

Thanks,
But I have to put this piece of code in a separate class out of web
page.
 
S

sloan

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.


...
 
S

Stoitcho Goutsev \(100\)

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*.
 
S

Stoitcho Goutsev \(100\)

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.
 

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

Top