C#会话变量仅在方法中有效

时间:2018-09-26 23:56:01

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

我正在我的ActionResult方法中创建会话变量,它在该方法中可以正常工作,但是每当我从同一控制器中的其他方法进行调用时,都会得到Null引用。我在这里阅读了很多问题,没有任何帮助。不知道是什么原因导致此问题。如果您对此有任何想法,我将非常感谢您的回答。

可能的帮助:

  • 检索是在用户单击“按钮”时发生的。但我认为它不会改变任何东西。
  • 在单击按钮时未调用的其他方法中,它也将不起作用。
  • 我检索到的方法是空方法。
  • 这肯定不是超时问题。我之前添加了超时,但没有帮助。

在此特定示例中,我使用的是List对象,但与我将分配给Session的任何其他对象的使用方法相同。

声明:

  List<Thread> tList = Session["tList"] as List<Thread>;
        if (tList == null)
        {
            tList = new List<Thread>();
            Session["tList"] = tList;
        }

通过其他方法失败:

 var t = new Thread(() =>
        {
            foreach (var item in Session["tList"] as List<Thread>)
            {
                try
                {
                    item.Abort();
                }
                catch (Exception)
                {
                }
            }
        });

web.config(整个system.web部分,因为可能还有其他东西阻止了它。不知道):

<authentication mode="Forms">
  <forms cookieless="UseCookies" loginUrl="~/user/login" slidingExpiration="true"></forms>
</authentication>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<httpModules></httpModules>
<sessionState cookieless="true" regenerateExpiredSessionId="true" />

0 个答案:

没有答案