我正在我的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" />