在什么情况下HttpContext.Current.Session可以为null?

The*_*ght 6 .net asp.net session httpcontext

在什么情况下HttpContext.Current.Session可以为null?

我有一些asp.net页面.

我只是想知道为什么我应该一直检查对象的Session对象?

例如

public static object GetSession(string key)
{

if (HttpContext.Current.Session != null)
{

return HttpContext.Current.Session[key];

}

return null;

}
Run Code Online (Sandbox Code Playgroud)

小智 0

如果 web.config 中的 sessionState 被关闭,它可能会为 null

 <configuration>
  <system.web>
    <sessionState mode="Off" />
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)