views:

226

answers:

1

My ASP.NET web application works fine in Internet Explorer, but in Firefox and Chrome I have a problem where HttpContext.Current is defined, but HttpContext.Current.Session is null. The error is probably caused by the fact that the Session object is being accessed to early in the ASP.NET page life cycle. But since the web site is primarely going to be accessed using Internet Explorer I'm hoping to find a quick, acceptable fix. Perhaps something along these lines:

if (HttpContext.Current.Session == null)
{
    // Create Session - but how?
}

So the question is: Can I create the Session object programmatically in ASP.NET?

Edit: The bug was apparantly related to a .designer file that was out of sync. Weird, but now it works.

+1  A: 

I think the problem that you actually have is that IE is the only browser accepting the Session cookie that ASP.NET uses to persist the session identifier.

Either ensure that all your browsers are accepting cookies or set up your ASP.NET application to use cookieless sessions.

The answer to your question however is no, you cannot create the session yourself. ASP.NET must do this on your behalf.

Andrew Hare
Thanks. I think I'll have to dig deeper to solve this problem. It is apparantly not consistent, and I have just been told, that it's probably only happening when using Visual Studio's internal web server, making this a non issue on the production server.
Jan Aagaard
In the context of the web application itself, you can't create a context/session, but there are ways to do this if you need it for unit testing. They are ugly and I'd using mocking on HttpContextBase/HttpSessionStateBase if possible. Just thought I'd add this for completion in case someone gets to this question/answer in the context of unit testing instead of an app.
tvanfosson
@Jan -- sometimes the session gets messed up if you are making changes to the application without restarting the web server. I often stop the actual web server (right-click on the taskbar icon) when I see session mess ups when running in the debugger. Usually clears the session related stuff right up.
tvanfosson