你的位置:首页 > ASP.net教程

[ASP.net教程]Session超时问题(AOP 过滤器)


public class TimeoutAttribute : ActionFilterAttribute  {    public override void OnActionExecuting(ActionExecutingContext filterContext)    {      HttpContextBase context = filterContext.HttpContext;      HttpResponseBase response = filterContext.HttpContext.Response;      HttpRequestBase request = filterContext.HttpContext.Request;      if (context.Session["User"] == null)      {        if (request.IsAjaxRequest())          response.Status = "401 Session Timeout";        else          response.Redirect("/Home/Login");//返回登录页        filterContext.Result = new HttpUnauthorizedResult();//这一行保证不再执行Action的代码        response.End();//必须加上这句,否则返回前台status始终是200        return;      }      base.OnActionExecuting(filterContext);    }  }


 本文参考 I鱿鱼的博客。。。。。