By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

HttpContext.Current.Session is null

P: n/a
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
}

May 16 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

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
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 16 May 2007 13:25:30 -0700, Dave <on********@gmail.comwrote:
>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.
May 16 '07 #2

P: n/a

"Dave" <on********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
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
May 16 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.