What is the difference between these 2 piece of codes.
HttpContext.Current.Session["myvariable"]
Session["myvariable"]
asp.net 4.0 and C# 4.0
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They’re effectively the same, in that they will access the same Session data.
The reason you can call
Sessionin your code-behind is because ASP.Net pages by default extend theSystem.Web.UI.Pagetype. This has aSessionpublic property. If you look at the code for this in Reflector you can see that it just callsHttpContext.Current.Sessionitself (through its ownContextproperty).In other classes you will not have access to that property, but you can use
HttpContext.Current.Sessionto access the session data instead, as long as you’re running in the context of a web application.