HttpContext.Current.Session is null

D

Dave

After some digging, I discovered HttpContext.Current.Session is null
when trying to access a session variable, username, in my upload.cs
code which is in the App_Code folder.

I just determined that I can't because HttpContext.Current.Session is
null. (HttpContext.Current is fine though)

I think there may be another server side method interfering with my
ability to access the session.

Is there any other way to share a variable with my default.aspx page?

If anyone can help, here is the code for upload.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Services;

public class Upload : IHttpHandler
{
string m_username;
public Upload()
{
// I've placed the code to set the session variable in different
locations
// no matter where I cannot get it. HttpContext.Current.Session is
NULL??
string m_username="";
if (null != HttpContext.Current && null !=
HttpContext.Current.Session &&
null != HttpContext.Current.Session["username"]){
m_username=HttpContext.Current.Session["username"].ToString();
m_username=m_username+"\\";
}
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
// get the applications path
string tempFile = context.Request.PhysicalApplicationPath;
// loop through all the uploaded files
for(int j = 0; j < context.Request.Files.Count; j++)
{
HttpPostedFile uploadFile = context.Request.Files[j];
if (uploadFile.ContentLength > 0)
{
uploadFile.SaveAs(string.Format("{0}{1}{2}",
tempFile, "Upload\\" + m_username , uploadFile.FileName));

}
}
}
HttpContext.Current.Response.Write(" ");
}

#endregion
}
 
S

Samuel R. Neff

Do you have anything in your web.config related to http modules? I
had this problem once when I added a <clear /> element to the http
modules not realizing that many of the asp.net functionality depended
on modules defined in the machine config.

HTH,

Sam
 
A

Alexey Smirnov

Dave said:
After some digging, I discovered HttpContext.Current.Session is null
when trying to access a session variable, username, in my upload.cs
code which is in the App_Code folder.

I just determined that I can't because HttpContext.Current.Session is
null. (HttpContext.Current is fine though)

I think there may be another server side method interfering with my
ability to access the session.

Is there any other way to share a variable with my default.aspx page?

If anyone can help, here is the code for upload.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Services;

public class Upload : IHttpHandler
{
string m_username;
public Upload()
{
// I've placed the code to set the session variable in different
locations
// no matter where I cannot get it. HttpContext.Current.Session is
NULL??
string m_username="";
if (null != HttpContext.Current && null !=
HttpContext.Current.Session &&
null != HttpContext.Current.Session["username"]){
m_username=HttpContext.Current.Session["username"].ToString();
m_username=m_username+"\\";
}
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
// get the applications path
string tempFile = context.Request.PhysicalApplicationPath;
// loop through all the uploaded files
for(int j = 0; j < context.Request.Files.Count; j++)
{
HttpPostedFile uploadFile = context.Request.Files[j];
if (uploadFile.ContentLength > 0)
{
uploadFile.SaveAs(string.Format("{0}{1}{2}",
tempFile, "Upload\\" + m_username , uploadFile.FileName));

}
}
}
HttpContext.Current.Response.Write(" ");
}

#endregion
}

You need to implement IRequiresSessionState or IReadOnlySessionState

e.g.

public class Upload : IHttpHandler, IReadOnlySessionState
 

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