会话变量抛出nullreferenceexception

时间:2014-08-18 17:02:16

标签: c# asp.net asp.net-mvc-4 iis session-variables

我有一个使用ASP MVC4的Web应用程序

我在

中设置了会话变量
protected void Session_Start()
    {
        Session["__UserLanguage"] = "EN";
    }

我在我的控制器上使用它

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

但是当我在IIS上发布它时它会返回

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

当我试图在我的控制器中获取它时

Session["__UserLanguage"] = language;

为什么? :'(

编辑:实际上调试后我的应用程序中没有使用Session_Start()......怎么可能?

添加

编辑2:

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

这解决了我的问题!为什么这不是由visual studio直接添加的:(

1 个答案:

答案 0 :(得分:1)

添加

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

这解决了我的问题!

相关问题