HttpContext.Current.Session is null

Discussion in 'ASP .Net' started by Dave, May 16, 2007.

  1. Dave

    Dave Guest

    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
    }
     
    Dave, May 16, 2007
    #1
    1. Advertising

  2. 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 <> wrote:

    >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.
     
    Samuel R. Neff, May 16, 2007
    #2
    1. Advertising

  3. "Dave" <> wrote in message
    news:...
    > 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
     
    Alexey Smirnov, May 16, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    732
    William F. Robertson, Jr.
    May 16, 2005
  2. =?Utf-8?B?RGF2ZQ==?=

    HttpContext.Current is null in asynch method?

    =?Utf-8?B?RGF2ZQ==?=, Oct 31, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,720
    Joerg Jooss
    Nov 2, 2005
  3. eric
    Replies:
    8
    Views:
    2,598
    kishore vanapalli
    Oct 2, 2007
  4. R.A.M.
    Replies:
    14
    Views:
    1,945
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Oct 17, 2007
  5. Tony

    Dim context As HttpContext = HttpContext.Current

    Tony, Mar 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    310
    Teemu Keiski
    Mar 3, 2004
Loading...

Share This Page