What is the difference between these 2 piece of codes.
HttpContext.Current.Session["myvariable"]
Session["myvariable"]
asp.net 4.0 and C# 4.0
What is the difference between these 2 piece of codes.
HttpContext.Current.Session["myvariable"]
Session["myvariable"]
asp.net 4.0 and C# 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).
There is no difference. Page.Session returns the HttpContext.Current.Session
With that being said, I've written .dll's that act as extensions for web applications. These .dll's have not concept of Session
. In these instances, I can access the current session of the web application that is using my .dll by referencing HttpContext.Current.Session
There's no difference. They are the same thing; the second form is shorter :)
There is no difference in behavior. If you are using code in your custom class where HttpContext is not directly available and want to access session value than we use first line of code, while second line is used when accessing in Page or control classes.
Another pretty thorough answer from Nicholas Carey https://stackoverflow.com/a/6021261/365017
"HttpApplication's Session property exhibits different behaviour than does that of the proporty HttpContext.Current.Session. They will both return a reference to the same HttpSessionState instance if one is available. They differ in what they do when there is no instance of HttpSessionState available for the current request.
Not all HttpHandlers provide session state. To do so, the HttpHandler must implement [one or both?] the marker interfaces IRequiresSessionState or IReadOnlySessionState.
HttpContext.Current.Session simply returns null if there is no session available.
The HttpApplication's implementation of the Session property throws an HttpException with the message Session state is not available in this context. rather than return a null reference."
Internally, Page.Session points to It's HttpContext.Current.Session only, but there are still two differences depending on from where it's called.
Page.Session can be accessed only from classes inherited from System.Web.UI.Page and it will throw HttpException when accessed from WebMethod.
Where as HttpContext.Current.Session can be accessed from anywhere as long as you're running in the context of a web application.
Other important difference where you can access Page.Session but cannot access HttpContext.Current.Session :
If there is a method named GetData in your page(inherited from System.Web.UI.Page) which is executed concurrently in different threads from some other page method, GetData method can access the Page.Seession, but you cannot access HttpContext.Current.Session.
It's because GetData has been called from different thread so HttpContext.Current is null and HttpContext.Current.Session will throw null reference exception, but Page.Session will still be attached with page object so page method GetData can access the Page.Session.