Web uygulamamda, oturum değişkenlerini okumak için şöyle bir şey yapıyorum:
if (HttpContext.Current.Session != null && HttpContext.Current.Session["MyVariable"] != null)
{
string myVariable= (string)HttpContext.Current.Session["MyVariable"];
}
HttpContext.Current.Session ["MyVariable"] 'ın neden boş olduğunu kontrol etmenin neden önemli olduğunu anlıyorum (değişken Oturumda henüz depolanmamış olabilir veya Oturum çeşitli nedenlerle sıfırlanmış olabilir), ama neden kontrol etmem gerekiyor? boşsa HttpContext.Current.Session?
Anladığım kadarıyla oturum ASP.NET tarafından otomatik olarak oluşturulur, bu nedenle HttpContext.Current.Session asla boş olmamalıdır. Bu varsayım doğru mu? Boş olabilirse, içine bir şey kaydetmeden önce onu da kontrol etmem gerektiği anlamına mı geliyor:
if (HttpContext.Current.Session != null)
{
HttpContext.Current.Session["MyVariable"]="Test";
}
else
{
// What should be done in this case (if session is null)?
// Is it possible to force the session to be created if it doesn't exist?
}