Take the 2-minute tour ×

Is this the correct way of accessing session variables in classes. I am not talking about aspx pages code behind. I am talking about the classes we made.

 HttpContext.Current.Session["myvariable"]="my variable";
share|improve this question
It depends. What do the classes do? –  SLaks Oct 30 '11 at 22:48
By the way, has anybody ever told you that your pages are classes? –  John Saunders Oct 30 '11 at 23:04
Slaks can i reach sessions variables without any problem inside global asax with this way ? John yes they are but it sometimes differentiates when accessing variables. –  MonsterMMORPG Oct 30 '11 at 23:13

3 Answers 3

up vote 1 down vote accepted

This code will work, but I recommend wrapping it in some property like this:

MyVariableType MyVariable
   get { return (MyVariable)(HttpContext.Current.Session["myvariable"] ?? SomeDefaultOrNullValue); }
   set { HttpContext.Current.Session["myvariable"] = value; }
share|improve this answer
hello. why do i need to wrap it ? can't i just test with if(HttpContext.Current.Session["myvariable"]!=null) ? –  MonsterMMORPG Oct 30 '11 at 22:58
@MonsterMMORPG. For better maintability. For example if you will want make some changes and store this variable in cache, you will need to change only the implementation of getter and setter of property instead of changing every reference to Session["Myvariable"]. Or you can easily add condition for testing if session != null etc. IMHO it's a good practice to encapsulate session variables –  Łukasz Wiatrak Oct 30 '11 at 23:04
thanks. i see there is no difference expect maintainability. can i use this httpcontext.current.session inside public static functions ? –  MonsterMMORPG Oct 30 '11 at 23:11
Yes, you can use it in static function –  Łukasz Wiatrak Oct 31 '11 at 7:01

That code will work.

However, unless your class are dedicated to web UI and will only be used by HTTP handlers, it's poor design; you should avoid coupling your backend logic to ASP.Net.

share|improve this answer
Hello. Thanks for the answer. But i did not understand your second sentence. I will use it at my website : monstermmorpg.com –  MonsterMMORPG Oct 30 '11 at 22:46
and i am going to use it inside global.asax file. is this work there ? thank you –  MonsterMMORPG Oct 30 '11 at 22:51
@MonsterMMORPG: He's saying you should wrap any calls to HttpContext.Current, or anything else that is session or HTTP handler related. See this question for why and how. Under the covers, yours is the mechanism you'd use, but you don't want to stick it directly in any of your business logic code - you want to abstract it so you can swap it out to allow your business logic code to be usable elsewhere, and allow unit tests to create the data in other ways. –  Merlyn Morgan-Graham Oct 30 '11 at 22:56
Merlyn Morgan-Graham it is too complicated for me i did not understand. why do i need to wrap it ? –  MonsterMMORPG Oct 30 '11 at 22:59
@MonsterMMORPG: If you program, nothing is too complicated for you. Learn everything (eventually). You asked if what you are doing is correct, and we've told you "yes, but we recommend you wrap it". And you don't need to, it is just a best practice. I've told you why. If you don't think those situations are valuable now, then hopefully you will at some point :) –  Merlyn Morgan-Graham Oct 30 '11 at 23:00

Yes, this is the best method to access the session object in classes.

share|improve this answer

Your Answer


