Nathan Taylor
Nathan Taylor

Reputation: 24606

ASP.NET MVC - Alternative to Using Session

I have an ASP.NET MVC view that uses jquery.Uploadify to post files to one of my controllers for use in my application and one of the side effects I noticed with Uploadify is that when the Flash file Uploadify uses to submit files to the server posts to my controller it gets its own SessionID from ASP.NET. This would be fine, of course, if my Upload controller didn't use the Session to store a list of files that have been uploaded by the current user for future manipulation by my application... So given this issue, after I upload files with my View, the current user's session does not contain any of the files that were just posted.

Any suggestions on how to achieve what I want without relying on Sessions (and, preferably, without a database)?

Upvotes: 3

Views: 4878

Answers (4)

Nathan Taylor
Nathan Taylor

Reputation: 24606

This is the solution I came up with. I haven't done much testing, but it seems to be an acceptable alternative to Session in my current scenario. I will use the Global.asax's Session_End/Session_Start to ensure rows are created and removed as needed.

public class UserTable : Dictionary<string, Dictionary<string, object>>
{
    public new object this[string key]
    {
        get
        {
            object value = null;
            if (HttpContext.Current != null)
            {
                var sessionId = HttpContext.Current.Session.SessionID;
                if (ContainsKey(sessionId) && base[sessionId].ContainsKey(key))
                    value = base[sessionId][key];
            }
            else
                throw new Exception("No HttpContext present.");
            return value;
        }
        set
        {
            if (HttpContext.Current != null)
            {
                var sessionId = HttpContext.Current.Session.SessionID;
                if (!ContainsKey(sessionId))
                    Add(sessionId, new Dictionary<string, object>());
                if (!base[sessionId].ContainsKey(key))
                    base[sessionId].Add(key, value);
                else
                    base[sessionId][key] = value;
            }
            else
                throw new Exception("No HttpContext present.");
        }
    }

    public object this[string sessionId, string key]
    {
        get
        {
            object value = null;
            if (ContainsKey(sessionId) && base[sessionId].ContainsKey(key))
                value = base[sessionId][key];
            return value;
        }
        set
        {
            if (!ContainsKey(sessionId))
                Add(sessionId, new Dictionary<string, object>());
            if (!base[sessionId].ContainsKey(key))
                base[sessionId].Add(key, value);
            else
                base[sessionId][key] = value;
        }
    }

    public void Add(string sessionId)
    {
        Add(sessionId, new Dictionary<string, object>());
    }

    public void Add()
    {
        if (HttpContext.Current != null)
            Add(HttpContext.Current.Session.SessionID);
        else
            throw new Exception("No HttpContext present.");
    }

    public new void Remove(string sessionId)
    {
        base.Remove(sessionId);
    }

    public void Remove()
    {
        if (HttpContext.Current != null)
            Remove(HttpContext.Current.Session.SessionID);
        else
            throw new Exception("No HttpContext present.");
    }
}

Upvotes: 0

Wyatt Barnett
Wyatt Barnett

Reputation: 15663

One thing to cross check--did you make the session "live" by adding some data to it before going to uploadify? ASP.NET regenerates sessions until it has data in the session.

Upvotes: 0

Freddy
Freddy

Reputation: 3304

Maybe a static hashmap which key is the user:ip of the client? The value will be whatever object you want to stored across the different Sessions.

Upvotes: 0

Chris Johnston
Chris Johnston

Reputation: 1919

Since Uploadify is purely a front end script, I don't understand why it would be getting a session from ASP.NET. I also don't fully understand what your particular problem is.

If your problem is that once the files are uploaded, the user can't see them on the screen, then I would suggest figuring out a method displaying the list of files that is independent of Uploadify. If it can, have it send an ID token along with the files and then grab the data needed to show the list from a database.

Upvotes: 2

Related Questions