asp.net - Error: Request is not available in this context -
i have code in global.asax
void application_start(object sender, eventargs e) { // code runs on application startup if (request.cookies["mylang"] == null) { httpcookie mylang = new httpcookie("mylang"); mylang.value = "fa"; mylang.expires = datetime.now.addyears(1); response.cookies.add(mylang); session.add("mylang", "fa"); } thread.currentthread.currentuiculture = new cultureinfo(request.cookies["mylang"].value); thread.currentthread.currentculture = cultureinfo.createspecificculture(request.cookies["mylang"].value); session["mylang"] = request.cookies["mylang"].value; }
but when run website below error shown:
request not available in context
why?
application_start called once before asp files processed. why request not available yet.
you want use application_beginrequest called on each request.
void application_beginrequest(object sender, eventargs e) { config.init(); // code runs on application startup if (request.cookies["mylang"] == null) { ... } }
application_start vs application_beginrequest event in global.aspx
Comments
Post a Comment