Asp.net 错误:请求在此上下文中不可用
我在global.asax中有这个代码Asp.net 错误:请求在此上下文中不可用,asp.net,global-asax,Asp.net,Global Asax,我在global.asax中有这个代码 void Application_Start(object sender, EventArgs e) { // Code that runs on application startup if (Request.Cookies["mylang"] == null) { HttpCookie mylang = new HttpCookie("mylang");
void Application_Start(object sender, EventArgs e)
{
// Code that 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;
}
但当我运行我的网站时,下面显示了错误:
请求在此上下文中不可用
为什么?在处理任何ASP文件之前,会调用一次应用程序启动。因此,请求还不可用 您希望使用应用程序\u BeginRequest,该应用程序在每次请求时都会被调用
void Application_BeginRequest(object sender, EventArgs e)
{
Config.Init();
// Code that runs on application startup
if (Request.Cookies["mylang"] == null)
{
...
}
}
在处理任何ASP文件之前,会调用一次应用程序启动。因此,请求还不可用 您希望使用应用程序\u BeginRequest,该应用程序在每次请求时都会被调用
void Application_BeginRequest(object sender, EventArgs e)
{
Config.Init();
// Code that runs on application startup
if (Request.Cookies["mylang"] == null)
{
...
}
}
你还没有收到请求吗?@JohnSaunders:我不明白你的问题对不起,我本想声明的。你在申请的时候不在申请中。你还没有要求吗?”约翰桑德斯:我不理解你的问题,对不起,我是想把这句话说出来。在应用程序启动时,您不在请求的中间。