asp.net mvc - Session state not available in Global.asax's AcquireRequestState nor PostAcquireRequestState events -
i have asp.net mvc application has per-user settings, including current culture settings. we're supposed set thread.currentthread.currentculture
within application_acquirerequeststate
or application_postacquirerequeststate
events of httpapplication
.
i want maintain users' settings in session state dictionary, inside application_acquirerequeststate
method observe:
httpcontext.current.session == null
- the quickwatch window reports
this.session
:((system.web.httpapplication)(this)).session'
threw exception of type'system.web.httpexception' system.web.sessionstate.httpsessionstate {system.web.httpexception}
"session state not available in context."
interestingly, httpcontext.current._sessionstatemodule == null
true, though have <sessionstate mode="inproc" />
in web.config file.
why session unavailable?
i ran problem , manage solve after studies. hope can help.
i'm unsure of part have <sessionstate mode="inproc" />
in web.config thou.
in case, need check if session["language"]
null in application_acquirerequeststate
. if it's not, code it. when run program, application_acquirerequeststate first place code go. @ point, session null. if writing session , breakpoint stepped through it, hit error.
according cycle, session state not ready in application_acquirerequeststate when run program.
later, after first page executed , had set session value, application_acquirerequeststate
called again , time had set session. error won't appear again.
to cater issue, had following complete code:
try { if (system.web.httpcontext.current.session != null) { if (system.web.httpcontext.current.session["language"] != null) { lang = system.web.httpcontext.current.session["language"].tostring(); } else { lang = "en"; } } else { lang = "en"; } } catch { lang = "en"; }
for code above, default language en, regardless of session state ready or not. if code hit session state error in try block, final lang value "en". know quite nasty way handle code case, can avoid session state error , ensure lang variable can return value.
hope somehow able help.
Comments
Post a Comment