frenchie
frenchie

Reputation: 51937

global asax file

What is the global asax file for? I'm looking to declare a user-specific dictionary of objects that'll be used throughout the pages of the application. Where do I declare this dictionary?

Thanks.

Upvotes: 3

Views: 3045

Answers (4)

Shekhar_Pro
Shekhar_Pro

Reputation: 18430

The Global.asax file is for defining application-level event handling. There are various events like application launch, session start... here is a link to MSDN documentation: http://msdn.microsoft.com/en-us/library/fwzzh56s.aspx.

For your scenario use the session start event for persisting information for a session.

Upvotes: 2

Mormegil
Mormegil

Reputation: 8071

Global.asax contains a class representing your application as a whole. If your dictionary is user-specific (i.e. not common to all users), you need to store it in a user-specific location. The first thing which comes to mind when you need to keep something user-specific is to use the session storage. (See MSDN.)

Added per comment:

public static class SessionObjects
{
    public static Dictionary<DateTime, List<int>> MySessionStoredLists
    {
            get
            {
                    var session = HttpContext.Current.Session;
                    if (session == null) throw new InvalidOperationException();
                    var fromSession = (Dictionary<DateTime, List<int>>)session["MySessionStoredLists"];
                    if (fromSession == null)
                    {
                            fromSession = new Dictionary<DateTime, List<int>>();
                            MySessionStoredLists = fromSession;
                    }
                    return fromSession;
            }
            private set
            {
                session["MySessionStoredLists"] = value;
            }
    }
}

This is just an example, but your idea seems a bit suspicious to me – do you want to keep something per-user per-date basis? Isn’t a full-featured persistent storage in a database better?

Upvotes: 5

davidsleeps
davidsleeps

Reputation: 9503

The global.asax file is an application class where you can handle various application level events. Link to MSDN.

If you are happy to store this dictionary in the Session (which is specific to each user), you could initialise it in the Session_Start event

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

Global.asax file is used to register some application global events like: application starts, request begins, an unhandled exception propagates, ... User-specific stuff should be stored into the Session.

Upvotes: 3

Related Questions