会话超时时调用Ajax

全部,如果在会话超时时进行ajax调用,我试图重定向到登录页面。 这是我到目前为止所做的。

为所有操作定义操作筛选器。

public class AuthenticateFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); var routeDataSet = filterContext.RouteData; if (LoginUser.LoginAdministrator == null) { //if the useinfo stored in session is timeout. if (routeDataSet != null && routeDataSet.Values["controller"] != null && routeDataSet.Values["controller"].ToString().ToLower().Equals("login") && routeDataSet.Values["action"] != null && routeDataSet.Values["action"].ToString().ToLower().Equals("login")) { //if it is login action itself.let it be. don't do anything. } else { //redirect to login page. filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { "controller", "Login" }, { "action", "Login" } }); } } } } 

当会话超时时,这适用于非ajax动作调用。 但是对于ajax电话。 它不能重定向到登录页面但只返回一个html页面字符串(似乎它是登录页面的源html代码),而不是真正的结果。 假设我们有这样的代码。

 function ajaxGetLogDetail(logId) { var sUrl = "/LogDetail/index?logId=" + logId; $.ajax({ cache: false, type: "GET", async: false, url: sUrl, success: function (result) { //please note result is html string. not the really result. }, error: function (xhr) { alert(xhr.responseText); } }); } 

谁能帮忙给我一些解决这个问题的线索? 谢谢。

更新

根据Mohsin和Dave的回答(谢谢你们两位),这是最终的解决方案。 请检讨一下。 谢谢。

 public class AuthenticateFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); var routeDataSet = filterContext.RouteData; if (LoginUser.LoginAdministrator == null) { //&& routeDataSet != null && routeDataSet.Values["controller"] != null //&& !routeDataSet.Values["controller"].ToString().ToLower().Equals("login") && routeDataSet.Values["action"] != null //&& !routeDataSet.Values["action"].ToString().ToLower().Equals("login") && !filterContext.HttpContext.Request.HttpMethod.ToLower().Equals("get")) if (routeDataSet != null && routeDataSet.Values["controller"] != null && routeDataSet.Values["controller"].ToString().ToLower().Equals("login") && routeDataSet.Values["action"] != null && routeDataSet.Values["action"].ToString().ToLower().Equals("login")) { } else { if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.Result = new JsonResult { Data = new { ErrorMessage = "SystemSessionTimeOut" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { "controller", "Login" }, { "action", "Login" } }); } } } } } 

在客户端:

 function ajaxGetLogDetail(logId) { var sUrl = "/LogDetail/index?logId=" + logId; $.ajax({ cache: false, type: "GET", async: false, url: sUrl, success: function (result) { if (result.ErrorMessage=="SystemSessionTimeOut") { windows.location="/Login/Login"; } else { //... } }, error: function (xhr) { alert(xhr.responseText); } }); } 

Ajax调用CAN不会返回任何类型的重定向。 在核心,AJAX调用只返回一个字符串。 没有那种将执行重定向的引擎。

您可以执行客户端重定向。 如果会话超时并且客户端的Controller方法返回false:

  if !(routeDataSet != null && routeDataSet.Values["controller"] != null && routeDataSet.Values["controller"].ToString().ToLower().Equals("login") && routeDataSet.Values["action"] != null && routeDataSet.Values["action"].ToString().ToLower().Equals("login")) { return Json(new { success = false, message = errorMessage }); } 

在你的AJAX error函数中:

  error: function (xhr) { alert(xhr.responseText); window.location='/Login/Login'; } 

旁注:您的目的地是’/登录/登录’或’/帐户/登录’