1.建一个类,如下
using System;
using System.Web.Mvc;
using System.Web.Routing;
namespace CheckInManagerSystem.Controllers
{
public class CheckLogin : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session != null)
{
if (filterContext.HttpContext.Session.IsNewSession)
{
var sessionCookie = filterContext.HttpContext.Request.Headers[“Cookie”];
if ((sessionCookie != null) && (sessionCookie.IndexOf(“ASP.NET_SessionId”, StringComparison.OrdinalIgnoreCase) >= 0))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = “Account”, Action = “LogOff” }));//这里是跳转到Account下的LogOff,自己定义
}
}
}
}
}
}
2.在每个用到session的Action前增加这个[CheckLogin()],如
[CheckLogin()]
public ActionResult Details(int userid)
{
ViewDataLoad();
return View(_context.GetUserById(userid));
}
这样就OK啦!不会再因为session过期而报红页面了!