MVC Ajax调用Action时-OnActionExecuting RedirectResult 无法跳转的处理办法

public class BaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (Session["userInfo"] == null)
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new HttpStatusCodeResult(499);
                    filterContext.HttpContext.Response.Write("/Desktop/Login/Index");
                    return;
                }
                else
                {
                    filterContext.Result = new RedirectResult("/Desktop/Login/Index");
                    return;
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }
}

(function ($) {
            var _ajax = $.ajax;
            $.ajax = function (opt) {
                var fn = {
                    error: function (XMLHttpRequest, textStatus, errorThrown) { },
                }
                if (opt.error)
                    fn.error = opt.error;
                var _opt = $.extend(opt, {
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        if (XMLHttpRequest.status == "499")
                            window.location.href = "/Desktop/Login/Index";
                        else
                            fn.error(XMLHttpRequest, textStatus, errorThrown);
                    }
                });
                _ajax(_opt);
            };
        })(jQuery);

        $.ajaxSetup({
            statusCode: {
                499: function (data) {
                    window.location.href = data.responseText;
                }
            }
        });

$.ajax({
                type: "POST",
                url: "/desktop/language/index",
                data: { language: getCookie("languageCode") },
                datatype: "json",
                success: function (returndata) {
                    //alert("second success");
                }
            });

 

 

 

 

 

上一篇:MVC过滤器:自定义异常过滤器


下一篇:MVC Filter过滤器(一)