asp.net – 在Application_BeginRequest中设置会话变量
发布时间:2020-12-15 21:08:00 所属栏目:asp.Net 来源:网络整理
导读:我使用ASP.NET MVC,我需要在Application_BeginRequest设置一个会话变量。问题是,在这一点对象HttpContext.Current.Session总是null。 protected void Application_BeginRequest(Object sender,EventArgs e){ if (HttpContext.Current.Session != null) { /
我使用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中的HTML清理程序,用于过滤危险
- asp.net-mvc-4 – 自定义多态模型绑定程序不绑定派生类型的
- asp.net-mvc – 集合的验证摘要
- ASP.net WebForms是CPU密集型平台吗?
- SSRS和asp.net – 在报表查看器中将参数从.net传递到ssrs
- ASP.NET:压缩ViewState
- asp.net-mvc-3 – 使用Windows身份验证获取WCF中当前正在交
- asp.net-mvc-4 – 如何在mvc布局中添加徽标?
- asp.net-mvc – 如何在ASP.NET MVC中禁用客户端和代理缓存?
- asp.net – repeater或listview vs concatenated html
推荐文章
站长推荐
热点阅读