0

I have a web application using ASP MVC4

I set my session variable in

protected void Session_Start()
    {
        Session["__UserLanguage"] = "EN";
    }

And I use it on my controller

private void AddTranslation()
{
   string language = Request.Form["Language"];
   if (language != null)
   {
       Session["__UserLanguage"] = language;
   }
   else
   {
       language = Session["__UserLanguage"] as string;
   }
}

but when i publish it on IIS it return

System.NullReferenceException: Object reference not set to an instance of an object.

when I try to get it in my controller

Session["__UserLanguage"] = language;

Why ? :'(

EDIT : In fact After debugging Session_Start() was not used in my application ... How that's possible ?

EDIT 2 : after adding

<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition=""  />

This resolve my problem ! Why that's not directly added by visual studio :(

Eugene Podskal
  • 9,882
  • 5
  • 29
  • 51
Hugo B.
  • 21
  • 4
  • 2
    1) Is Session enabled on that production application? 2) Application restarts will flush in-process session. Your code has to be able to handle that. – David Crowell Aug 18 '14 at 17:04
  • See [this post](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). – Brian Aug 18 '14 at 17:05
  • 1)Yeap Session State was enabled in IIS with a 30 min timeout. 2) Normaly "Session_Start()" will allways put "EN" in Session["__UserLanguage"]! – Hugo B. Aug 18 '14 at 17:10
  • About how to use this site: do not add answers inside the question, post a self-answer. – Henk Holterman Aug 21 '14 at 10:27
  • 1
    @HugoB. Adding -resolved- to the tile is not considered as a good practice - http://meta.stackexchange.com/questions/116101/is-it-ok-to-add-solved-to-the-title-of-a-question. If you have found a solution, then you can post it as an answer for your own question and mark it as accepted. – Eugene Podskal Aug 21 '14 at 12:06

1 Answers1

1

after adding

<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition=""  />

This resolve my problem!

Hugo B.
  • 21
  • 4