我通过以下方式检测到会话到期:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (null != filterContext.HttpContext.Session)
{
// Check if we have a new session
if (filterContext.HttpContext.Session.IsNewSession)
{
string cookie = filterContext.HttpContext.Request.Headers["Cookie"];
// Check if session has timed out
if ((null != cookie) && (cookie.IndexOf("ASP.NET_SessionId") >= 0))
{
RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("action", "SessionExpired");
redirectTargetDictionary.Add("controller", "Error");
redirectTargetDictionary.Add("timeout", "true");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
return;
}
}
else { //continue with action as usual
base.OnActionExecuting(filterContext);
}
}
}
问题在于,每次运行应用程序时,都会触发SessionExpired
操作(当我第一次请求应用程序URL时,它会在cookie中找到ASP.NET_SessionId
字符串),然后是我加载另一个URL应用程序工作正常。如何解决?