我有一个动作过滤器检查是否ID
设置了会话变量。出于开发目的,我在检查之前手动设置了这个变量。
public class MyActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext context)
{
context.HttpContext.Session.Add("ID", 123123);
int ID = (int)context.HttpContext.Session.Contents["ID"];
var rd = context.HttpContext.Request.RequestContext.RouteData;
TED _db = new TED();
//if not in DB
if (_db.Users.Find(ID) == null && rd.GetRequiredString("action") != "NoAccess")
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", "NoAccess");
redirectTargetDictionary.Add("controller", "Home");
redirectTargetDictionary.Add("area", "");
context.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
base.OnActionExecuted(context);
}
}
据我了解,此代码在任何页面之前运行,Session["ID"]
始终设置。如果我一直在测试,该站点运行良好,但如果我将其放置一段时间然后尝试继续测试,它似乎会中断。这是我得到的错误:
int UserID = (int)Session.Contents["ID"];
System.NullReferenceException: Object reference not set to an instance of an object.
最初我认为会话可能只是到期,但在任何页面加载之前,Session["ID"]
应该设置。这里有什么问题?