Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, the Session object might be null, but only in certain circumstances, which you will only rarely run into:</p> <ul> <li>If you have disabled the <a href="http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatemodule.aspx" rel="noreferrer">SessionState http module</a>, disabling sessions altogether</li> <li>If your code runs before the <a href="http://msdn.microsoft.com/en-us/library/system.web.httpapplication.acquirerequeststate.aspx" rel="noreferrer">HttpApplication.AcquireRequestState</a> event.</li> <li>Your code runs in an <a href="http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.aspx" rel="noreferrer">IHttpHandler</a>, that does not specify either the <a href="http://msdn.microsoft.com/en-us/library/system.web.sessionstate.irequiressessionstate.aspx" rel="noreferrer">IRequiresSessionState</a> or <a href="http://msdn.microsoft.com/en-us/library/system.web.sessionstate.ireadonlysessionstate.aspx" rel="noreferrer">IReadOnlySessionState</a> interface. </li> </ul> <p>If you only have code in pages, you won't run into this. Most of my ASP .NET code uses Session without checking for null repeatedly. It is, however, something to think about if you are developing an IHttpModule or otherwise is down in the grittier details of ASP .NET.</p> <h1>Edit</h1> <p>In answer to the comment: Whether or not session state is available depends on whether the AcquireRequestState event has run for the request. This is where the session state module does it's work by reading the session cookie and finding the appropiate set of session variables for you. </p> <p>AcquireRequestState runs before control is handed to your Page. So if you are calling other functionality, including static classes, from your page, you should be fine. </p> <p>If you have some classes doing initialization logic during startup, for example on the Application_Start event or by using a static constructor, Session state might not be available. It all boils down to whether there is a current request and AcquireRequestState has been run.</p> <p>Also, should the client have disabled cookies, the Session object will still be available - but on the next request, the user will return with a new empty Session. This is because the client is given a Session statebag if he does not have one already. If the client does not transport the session cookie, we have no way of identifying the client as the same, so he will be handed a new session again and again.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload