asp.net – 在Application_BeginRequest中设置会话变量
发布时间:2020-05-22 11:56:29 所属栏目: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 – HttpContext.Error vs HttpContext.Server.GetL
- asp.net – 选择框更改事件中的setTimeout
- asp.net – 使用回发解析.Net页面
- asp.net – 如何通过代码有选择地禁用浏览器文本输入中的自
- asp.net – 会话 – 多个浏览器选项卡 – 不同的会话?
- asp.net-mvc – ASP.NET MVC中的CSS和Javascript相对路径混
- asp.net-mvc – 将ASP.NET MVC应用程序部署到IIS7并保持干净
- asp.net-mvc-3 – MVC 3 – Html.EditorFor似乎缓存旧值$.a
- asp.net – 如何使用GridView和ObjectDataSource排序?
- asp.net – 在新的MVC4应用程序中获取UserId(int)
推荐文章
站长推荐
- asp.net – 如何在iframe中提交后刷新iframe父页
- ASP.net RequiredFieldValidator VisualStudio 2
- asp.net-web-api2 – SwashBuckle / Swagger –
- 是否有必要创建ASP.NET 4.0 SQL会话状态数据库,与
- ASP.net会话请求排队
- asp.net – Dapper.net交易问题
- SimpleMembership与ASP.NET MVC 4中的自定义数据
- asp.net-mvc – 使用Visual Studio和ASP.NET MVC
- 如何在ASP.NET MVC中禁用HTTP Keep-Alive?
- asp.net-mvc – MVC 4捕获所有路由从未到达
热点阅读
