अपने वेब आवेदन में, मैं सत्र चर को पढ़ने के लिए कुछ इस तरह करते हैं:यदि वर्तमान एएसपी.NET सत्र शून्य है तो मुझे क्या करना चाहिए?
if (HttpContext.Current.Session != null && HttpContext.Current.Session["MyVariable"] != null)
{
string myVariable= (string)HttpContext.Current.Session["MyVariable"];
}
मुझे समझ में क्यों यह जाँच करने के लिए क्यों HttpContext.Current.Session [ "MyVariable"] रिक्त है महत्वपूर्ण है (चर नहीं हो सकता है सत्र में अभी तक संग्रहीत किया गया है या सत्र विभिन्न कारणों से रीसेट कर दिया गया है), लेकिन मुझे यह जांचने की आवश्यकता क्यों है कि HttpContext.Current.Session
शून्य है?
मेरी समझ यह है कि सत्र स्वचालित रूप से एएसपी.NET द्वारा बनाया जाता है इसलिए HttpContext.Current.Session को शून्य नहीं होना चाहिए। क्या यह धारणा सही है? यह शून्य हो सकते हैं, यह मैं भी इसमें कुछ भंडारण से पहले यह जाँच करनी चाहिए मतलब है:
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 विभिन्न व्यवहार होगा, आप इसे [एक्सेस करना सत्र ASP.NET वेब API का उपयोग करना] (http://stackoverflow.com/questions/9594229/accessing-session-using पर देख सकते हैं -asp-net-web-api? answertab = वोट # टैब-टॉप) –