会话超时时调用Ajax

时间:2013-02-21 11:36:09

标签: jquery ajax asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

全部,如果在会话超时时进行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);
            }
        });
    }

1 个答案:

答案 0 :(得分:9)

Ajax调用不能返回任何类型的重定向。在核心,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';
        }

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

相关问题