In my asp.net MVC application, I want to add a variable (a flag for the kind of system a user has) that can be accessed whenever a page loads or the user performs certain actions. I have decided to add a session variable for this (does this seem reasonable?), and I just need to grab the flag from a table in the database. My plan was to set the variable on Session_Start, but this doesn’t appear to be the right way to do it as I need to query the database and I’m not sure if I should from the Global.asax. Where should I be populating this variable? Or is there a better way to do it?
Thanks in advance!
Session variable is a reasonable choice and session_start can be a place to get the value. However, if the value is user specific then in such case, you need user identity. Authentication will establish user’s identity and not session start (both are independent in ASP.NET) – so instead of
session_start, better bet would beApplication_AcquireRequestStatewhere you should check if user is authenticated or not and if yes, then check if your session variable has been set or not. If not set then you can get the value from database.A slight variation would be on-demand loading i.e. create a wrapper method to get the flag value. Wrapper method will check if value has already been retrieved or not – if not then it will fetch it and cache the value in suitable store (e.g. session state).