제 웹 응용 프로그램에서 세션 변수를 읽는 것과 같은 일을합니다.현재 ASP.NET 세션이 null 인 경우 어떻게해야합니까?
if (HttpContext.Current.Session != null && HttpContext.Current.Session["MyVariable"] != null)
{
string myVariable= (string)HttpContext.Current.Session["MyVariable"];
}
왜 HttpContext.Current.Session [ "MyVariable"]이 null인지를 확인하는 것이 중요합니다. 아직 세션에 저장되었거나 세션이 여러 가지 이유로 재설정되었습니다.)하지만 HttpContext.Current.Session
가 null인지 확인해야하는 이유는 무엇입니까?
제 생각에 세션이 ASP.NET에 의해 자동으로 만들어 지므로 HttpContext.Current.Session이 절대로 null이 아니어야합니다. 이 가정이 맞습니까? null이 될 수있는 경우 뭔가를 저장하기 전에 확인해야한다는 의미입니까?
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?
}
ASP.NET WebApi가 가지 다른 행동을해야합니다, 당신은 (http://stackoverflow.com/questions/9594229/accessing-session-using [세션 ASP.NET 웹 API를 사용하여 액세스]에 확인할 수 있습니다 -asp-net-web-api? answertab = votes # tab-top) –