Set session variable in Application_BeginRequest

2019-01-23 01:18发布

I'm using ASP.NET MVC and I need to set a session variable at Application_BeginRequest. The problem is that at this point the object HttpContext.Current.Session is always 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());
    }
}

3条回答
太酷不给撩
2楼-- · 2019-01-23 01:33

Try AcquireRequestState in Global.asax. Session is available in this event which fires for each request:

void Application_AcquireRequestState(object sender, EventArgs e)
{
    // Session is Available here
    HttpContext context = HttpContext.Current;
    context.Session["foo"] = "foo";
}

Valamas - Suggested Edit:

Used this with MVC 3 successfully and avoids session error.

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    HttpContext context = HttpContext.Current;
    if (context != null && context.Session != null)
    {
        context.Session["foo"] = "foo";
    }
}
查看更多
叛逆
3楼-- · 2019-01-23 01:38

Maybe you could change the paradigm... Perhaps you can use another property of the HttpContext class, more specifically HttpContext.Current.Items as shown below:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    HttpContext.Current.Items["__MySessionVariable"] = new object();
}

It won't store it on the session, but it will be stored on the Items dictionary of the HttpContext class and will be available for the duration of that specific request. Since you're setting it at every request, it would really make more sense to store it into a "per session" dictionary which, incidentally, is exactly what the Items is all about. :-)

Sorry to try to infer your requirements instead of answering your question directly, but I've faced this same problem before and noticed that what I needed was not the Session at all, but the Items property instead.

查看更多
叛逆
4楼-- · 2019-01-23 01:44

You can use the session items in Application_BeginRequest this way:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        //Note everything hardcoded, for simplicity!
        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("LanguagePref");

        if (cookie == null)
            return;
        string language = cookie["LanguagePref"];

        if (language.Length<2)
            return;
        language = language.Substring(0, 2).ToLower();   
        HttpContext.Current.Items["__SessionLang"] = language;
        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(language);

    }

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context != null && context.Session != null)
        {
            context.Session["Lang"] = HttpContext.Current.Items["__SessionLang"];
        }
    }
查看更多
登录 后发表回答