asp.net – 在Application_BeginRequest中设置会话变量
发布时间:2020-07-22 06:14:32 所属栏目:asp.Net 来源:互联网
导读:我使用ASP.NET MVC,我需要在Application_BeginRequest设置一个会话变量。问题是,在这一点对象HttpContext.Current.Session总是null。 protected void Application_BeginRequest(Object sender, EventArgs e){ if (HttpContext.Current.Sessi
|
我使用ASP.NET MVC,我需要在Application_BeginRequest设置一个会话变量。问题是,在这一点对象HttpContext.Current.Session总是null。 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());
}
}
解决方法在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";
}
} (编辑:长春站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – MVC 3布局页面,Razor模板和下拉列表
- asp.net-mvc – MVC项目中的System.Globalization.CultureN
- asp.net-mvc-routing – 在MVC 6控制器中使用urlhelper生成
- asp.net中MVC借助Iframe实现无刷新上传文件实例
- asp.net web大文件上传带进度条实例代码
- asp.net – MVC4 – ContextDependentView – 这是什么意思
- asp.net-mvc – 在我的ASP.NET MVC网站中缓存不能正常工作?
- asp-classic – 哪里可以存储经典ASP的连接字符串?
- ASP.Net 2中的上传文件在哪里?
- asp.net-mvc – 不要在ASP .NET MVC 4 BundleConfig中缩小某
