asp.net – 在Application_BeginRequest中设置会话变量
发布时间:2020-07-21 13:02:18 所属栏目: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 – 如何在asp.net mvc中处理分页?
- ASP.NET 2.0或3.5?
- asp.net-mvc – 如何检测移动浏览器,并将适当的内容指向它?
- 谈谈ASP.NET Core中的ResponseCaching
- .net – ELMAH对企业库异常处理块
- asp.net – 多线程环境中的文件访问策略(Web App)
- asp.net – 单个应用程序中的多个母版页
- ASP.NET MVC4 Razor模板简易分页效果
- asp.net-mvc – ASP.NET捆绑/分类:包括动态生成的Javascri
- asp.net-mvc – 在MVC Razor视图中使用@RenderBody有什么意
推荐文章
站长推荐
热点阅读