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); } }
前台:
error:function(a,b,c){
if(c.statusText=="Session Timeout"){
location="/home/login";
}
}
本文参考 I鱿鱼的博客。。。。。