4

I have a bilingual MVC 3 application, I use cookies and session to save "Culture" in Session_start method inside Global.aspx.cs file, but direct after it, the session is null.

This is my code:

    protected void Session_Start(object sender, EventArgs e)
    {
        HttpCookie aCookie = Request.Cookies["MyData"];

        if (aCookie == null)
        {
            Session["MyCulture"] = "de-DE";
            aCookie = new HttpCookie("MyData");
            //aCookie.Value = Convert.ToString(Session["MyCulture"]);
            aCookie["MyLang"] = "de-DE";
            aCookie.Expires = System.DateTime.Now.AddDays(21);
            Response.Cookies.Add(aCookie);
        }
        else
        {
            string s = aCookie["MyLang"];
            HttpContext.Current.Session["MyCulture"] = aCookie["MyLang"];
        }
 }

and second time it goes into the "else clause" because the cookie exists; inside my Filter, when it tries set the culutre, Session["MyCulture"] is null.

   public void OnActionExecuting(ActionExecutingContext filterContext)
    {

        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(HttpContext.Current.Session["MyCulture"].ToString());
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(HttpContext.Current.Session["MyCulture"].ToString());
    }
BenMorel
  • 34,448
  • 50
  • 182
  • 322
user217648
  • 3,338
  • 9
  • 37
  • 61
  • 1
    Related question with correct answer: [Calling the Session before any Controller Action is run in MVC](http://stackoverflow.com/questions/3263936/calling-the-session-before-any-controller-action-is-run-in-mvc) @Darin [already posted this solution](http://stackoverflow.com/questions/7705802/httpcontext-current-session-is-null-in-mvc-3-appplication/7705818#7705818) just adding it for reference. – Sergey Kudriavtsev Oct 09 '11 at 19:16

1 Answers1

13

Why are you using HttpContext.Current in an ASP.NET MVC application? Never use it. That's evil even in classic ASP.NET webforms applications but in ASP.NET MVC it's a disaster that takes all the fun out of this nice web framework.

Also make sure you test whether the value is present in the session before attempting to use it, as I suspect that in your case it's not HttpContext.Current.Session that is null, but HttpContext.Current.Session["MyCulture"]. So:

public void OnActionExecuting(ActionExecutingContext filterContext)
{
    var myCulture = filterContext.HttpContext.Session["MyCulture"] as string;
    if (!string.IsNullOrEmpty(myCulture))
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(myCulture);
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(myCulture);
    }
}

So maybe the root of your problem is that Session["MyCulture"] is not properly initialized in the Session_Start method.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you very much, Yes the root of the problem is the aCookie is not null but I cann't get the value of it. becuase first time (when i delete all cookies of the browser) and cookie is null, I don't get problem and session is not null. – user217648 Oct 09 '11 at 20:31
  • 3
    You say it's evil and should never be used. Care to elaborate on the alternatives? – Megacan Jan 13 '12 at 11:39
  • 5
    @Megacan, the alternative is provided in my answer: `filterContext.HttpContext.Session` instead of `HttpContext.Current.Session`. – Darin Dimitrov Jan 13 '12 at 12:32
  • 3
    @DarinDimitrov, are you saying `HttpContex.Current` is bad only in action filters? your answer more broadly states "Why are you using `HttpContext.Current` in an ASP.NET MVC application? Never use it." – Brad May 12 '15 at 21:24
  • 2
    @DarinDimitrov can you please explain *why* you believe HttpContext.Current is bad - providing the alternative is great but you have the power to help others gain a better understanding by explaining the reason for the alternative... – JTech Sep 15 '16 at 07:24