22

What is the difference between these 2 piece of codes.

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

asp.net 4.0 and C# 4.0

Yeldar Kurmangaliyev
  • 30,498
  • 11
  • 53
  • 87
MonsterMMORPG
  • 20,310
  • 69
  • 183
  • 306

8 Answers8

26

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.

Kasaku
  • 2,144
  • 16
  • 26
  • 6
    Well the object the properties return is the same, but if a session doesn't exist HttpContect.Current.Session will return null while Page.Session will throw an HttpException. – Polymorphix Nov 08 '12 at 12:15
10

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.

Variant
  • 16,291
  • 4
  • 37
  • 64
5

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).

Saeed Neamati
  • 33,583
  • 40
  • 128
  • 183
4

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

James Hill
  • 56,052
  • 18
  • 138
  • 155
2

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."

Community
  • 1
  • 1
Steve G.
  • 138
  • 1
  • 4
  • 9
2

There's no difference. They are the same thing; the second form is shorter :)

Icarus
  • 60,193
  • 14
  • 91
  • 110
2

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.

Nps
  • 1,568
  • 4
  • 18
  • 37
0

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.

Jay Shah
  • 2,780
  • 1
  • 20
  • 22