Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these two HttpContext.Current.Session and Session - asp.net 4.0

Tags:

What is the difference between these 2 piece of codes.

HttpContext.Current.Session["myvariable"] Session["myvariable"] 

asp.net 4.0 and C# 4.0

like image 639
MonsterMMORPG Avatar asked Oct 31 '11 14:10

MonsterMMORPG


1 Answers

They're effectively the same, in that they will access the same Session data.

The reason you can call Session in your code-behind is because ASP.Net pages by default extend the System.Web.UI.Page type. This has a Session public property. If you look at the code for this in Reflector you can see that it just calls HttpContext.Current.Session itself (through its own Context property).

In other classes you will not have access to that property, but you can use HttpContext.Current.Session to access the session data instead, as long as you're running in the context of a web application.

like image 79
Kasaku Avatar answered Oct 12 '22 07:10

Kasaku