asp.net – 在Application_BeginRequest中设置会话变量
发布时间:2020-07-21 10:04:50 所属栏目: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-3 – 使用自定义VirtualPathProvider加载嵌入式
- asp.net – 如何从TableAdapter中检索存储过程返回值
- asp.net – 如何使一个TextBox控件是多行不可调整大小?
- ASP.NET SQL成员资格表
- ASP.NET虚拟路径映射到另一个不允许的应用程序
- 在asp.net中将用户变量存储在数据库与会话中
- ASP.NET和System.Diagnostics跟踪 – 我错过了什么,或者这是
- asp.net-mvc-3 – Telerik MVC网格,在运行时从集合或字典中
- asp.net-mvc – MVC4部分视图没有将值加载到“容器”模型中
- 编辑并在ASP.NET Web项目中继续
推荐文章
站长推荐
- asp.net – 我如何使用AJAX来确定用户的会话是否
- asp.net-mvc-3 – MVC3剃须刀:是否可以渲染传统
- 有标签的ASP.NET WebControl吗?
- asp.net-mvc – 如何设置AntiForgeryToken cooki
- ASP.Net – AJAX UpdatePanel中的Javascript
- asp.net-mvc – 使用AD的ASP.NET MVC表单Auth在本
- asp.net-mvc-3 – 为MVC3应用程序配置Ninject的正
- ASP.NET两个截取字符串的方法分享
- asp.net – IE8 Win7 Facebook Connect问题
- asp.net-mvc-3 – 如何从ASP.NET MVC#输出中删除
热点阅读