What is the difference between these two HttpContext.Current.Session and Session - asp.net 4.0
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.
On a stantard scenario they are the same. The difference is that the first statement will also work in static contexts such as a WebMethod.
There is a difference. The second one (Session
) is a property of many .NET objects, like Page
for example. So, you can't have access to it, in the constructor of those objects for example. However, the first one (HttpContext.Current.Session
), is always ready and at your disposal (of course, after the session is loaded in the Request Processing Pipeline).