2011-05-12 10 views
36

में सत्र चर सेट करें I ASP.NET MVC का उपयोग कर रहा हूं और मुझे Application_BeginRequest पर सत्र चर सेट करने की आवश्यकता है। समस्या यह है कि इस बिंदु पर ऑब्जेक्ट HttpContext.Current.Session हमेशा null है।Application_BeginRequest

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    if (HttpContext.Current.Session != null) 
    { 
     //this code is never executed, current session is always null 
     HttpContext.Current.Session.Add("__MySessionVariable", new object()); 
    } 
} 
+0

संबंधित/प्रकार: http://stackoverflow.com/questions/765054/whens-the-earliest-i-can -access-some-session-data-in-global-asax/ –

उत्तर

64

Global.asax में AcquireRequestState आज़माएं। सत्र इस घटना है जो प्रत्येक अनुरोध के लिए आग में उपलब्ध है:

void Application_AcquireRequestState(object sender, EventArgs e) 
{ 
    // Session is Available here 
    HttpContext context = HttpContext.Current; 
    context.Session["foo"] = "foo"; 
} 

Valamas - सुझाए गए संपादित करें:

MVC 3 सफलतापूर्वक के साथ इस प्रयुक्त और सत्र त्रुटि बचा जाता है।

protected void Application_AcquireRequestState(object sender, EventArgs e) 
{ 
    HttpContext context = HttpContext.Current; 
    if (context != null && context.Session != null) 
    { 
     context.Session["foo"] = "foo"; 
    } 
} 
+7

मैं एमवीसी 3 का उपयोग कर रहा हूं और यह काम नहीं करता है। सत्र शून्य है। –

+2

यह वही है जो मुझे चाहिए था। –

+4

यह मेरे लिए या तो संदर्भ के लिए काम नहीं करता है। सत्र शून्य है – JBeckton

11

शायद तुम प्रतिमान बदल सकता है ... शायद आप HttpContext वर्ग का एक और संपत्ति का उपयोग कर सकते, और अधिक विशेष HttpContext.Current.Items के रूप में bellow दिखाया गया है:

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    HttpContext.Current.Items["__MySessionVariable"] = new object(); 
} 

यह संग्रहीत नहीं करेगा यह सत्र पर है, लेकिन इसे HttpContext क्लास के आइटम डिक्शनरी पर संग्रहीत किया जाएगा और उस विशिष्ट अनुरोध की अवधि के लिए उपलब्ध होगा। चूंकि आप इसे प्रत्येक अनुरोध पर सेट कर रहे हैं, इसलिए यह वास्तव में इसे "प्रति सत्र" शब्दकोश में संग्रहीत करने के लिए और अधिक समझ में आता है, जो संयोग से, आइटम बिल्कुल ठीक है। :-)

सीधे अपने प्रश्न का उत्तर देने की बजाय अपनी आवश्यकताओं का अनुमान लगाने का प्रयास करने के लिए खेद है, लेकिन मुझे पहले इसी समस्या का सामना करना पड़ा और देखा कि मुझे जो चाहिए वह सत्र नहीं था, बल्कि इसके बजाय आइटम संपत्ति थी।

4

आप Application_BeginRequest में इस तरह सत्र आइटम का उपयोग कर सकते हैं: का डुप्लिकेट की

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     //Note everything hardcoded, for simplicity! 
     HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("LanguagePref"); 

     if (cookie == null) 
      return; 
     string language = cookie["LanguagePref"]; 

     if (language.Length<2) 
      return; 
     language = language.Substring(0, 2).ToLower(); 
     HttpContext.Current.Items["__SessionLang"] = language; 
     Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(language); 

    } 

    protected void Application_AcquireRequestState(object sender, EventArgs e) 
    { 
     HttpContext context = HttpContext.Current; 
     if (context != null && context.Session != null) 
     { 
      context.Session["Lang"] = HttpContext.Current.Items["__SessionLang"]; 
     } 
    } 
संबंधित मुद्दे

 संबंधित मुद्दे