Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 防止在会话超时MVC4时打开jQuery模式弹出窗口_Asp.net Mvc_Jquery_Modal Dialog - Fatal编程技术网

Asp.net mvc 防止在会话超时MVC4时打开jQuery模式弹出窗口

Asp.net mvc 防止在会话超时MVC4时打开jQuery模式弹出窗口,asp.net-mvc,jquery,modal-dialog,Asp.net Mvc,Jquery,Modal Dialog,我有一个ASP.NETMVC4应用程序,其中我在会话超时过滤器处理程序中捕获会话超时,以处理会话超时。我还想处理ajax请求的会话超时。我最初实现了在本文中找到的代码 它最初对自动完成和其他ajax调用起到了作用,但唯一的问题是对模态弹出窗口的ajax调用 现在我将会话超时处理程序更改为如下所示: public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext

我有一个ASP.NETMVC4应用程序,其中我在会话超时过滤器处理程序中捕获会话超时,以处理会话超时。我还想处理ajax请求的会话超时。我最初实现了在本文中找到的代码

它最初对自动完成和其他ajax调用起到了作用,但唯一的问题是对模态弹出窗口的ajax调用

现在我将会话超时处理程序更改为如下所示:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (filterContext.HttpContext.Session != null)
    {
        if (filterContext.HttpContext.Session.IsNewSession)
        {
            var sessionStateDetails =(SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState");
            var sessionCookie = filterContext.HttpContext.Request.Headers["Cookie"];

            if ((sessionCookie != null) && (sessionCookie.IndexOf(sessionStateDetails.CookieName) >= 0))
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.HttpContext.Response.Clear();
                    filterContext.HttpContext.Response.StatusCode = 500;
i、 e.将状态代码设置为500(401对我不起作用,因为我正在使用Windows身份验证和模拟,因此如果我将状态更改为401,我将获得密码和用户名的安全弹出窗口)

然后,我在.ajaxSetup或.ajaxserror中捕获该错误,我已尝试了这两种方法…并重新引导到会话超时操作(根据需要)以显示会话超时视图

$(document).ajaxError(function (xhr, props) {
    if (props.status === 500) {
        ////session redirect goes here 
        var pathArray = window.location.pathname.split('/');
        var segment_1 = pathArray[1];
        var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/SessionTimeout";
        window.location = newURL;
    }
});
问题是在重新定向之前,ajax弹出窗口仍然会短暂打开。所以看起来不太好。你知道我怎样才能阻止它打开并顺利重新定向吗

以下是我的jQuery对话框代码:

$(document).ready(function () {
    $(".openDialog").live("click", function (e) {
        e.preventDefault();
        $("<div></div>")
            .addClass("dialog")
            .attr("id", $(this)
            .attr("data-dialog-id"))
            .appendTo("body")
            .dialog({
                title: $(this).attr("data-dialog-title"),
                close: function () { $(this).remove() },
                width: 600,
                modal: true,
                height: 'auto',
                show: 'fade',
                hide: 'fade',
                resizable: 'false'
            })
        .load(this.href);

    });

    $(".close").live("click", function (e) {
        e.preventDefault();
        $(this).closest(".dialog").dialog("close");
    });
});
$(文档).ready(函数(){
$(“.openDialog”).live(“单击”,函数(e){
e、 预防默认值();
$("")
.addClass(“对话框”)
.attr(“id”,$(此)
.attr(“数据对话框id”))
.附件(“正文”)
.对话({
标题:$(this.attr(“数据对话框标题”),
关闭:函数(){$(this).remove()},
宽度:600,
莫代尔:是的,
高度:“自动”,
节目:“褪色”,
隐藏:“褪色”,
可调整大小:“false”
})
.load(this.href);
});
$(“.close”).live(“单击”),函数(e){
e、 预防默认值();
$(此).close(.dialog”).dialog(“关闭”);
});
});

如果您有任何帮助,我将不胜感激。

删除您的覆盖
公共覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
并使用(在global.asax中):

在JavaScript中:

$.ajaxSetup({ //jQuery.ajaxSetup({
    statusCode: {
        302: function () {
            __userLoginEvent = true;
        }
    }
});

$(document).ajaxStop(function () {
    if (__userLoginEvent) {
        //your redirection here

        __userLoginEvent = false;
    }
})

我使用
ajaxStop
,因为如果你有多个ajax调用,你需要一个重定向。这对我来说是完美的工作。

我最终设法让它工作起来,如果其他人也有同样的问题,下面是我所做的。。。我一直在使用会话处理程序,但我现在提出的是403而不是500,因为提出500显然是错误的,并且有一些不良副作用:

代码来自

                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.HttpContext.Response.Clear();
                    filterContext.HttpContext.Response.StatusCode = 403;

                }
                else ...usual code goes here ....
然后我捕获ajax错误,如下所示:

$(document).ajaxError(function (xhr, props) {
    if (props.status == 403 ) {
        ////session redirect goes here 
        var pathArray = window.location.pathname.split('/');
        var segment_1 = pathArray[1];
        var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/SessionTimeout";
        window.location = newURL;
    }
});

当然,在重新定向之前,我的弹出窗口仍会尝试加载最短的时间,但我的用户似乎并不介意。

我有一个解决方案,但它有点不同:如果您调用ajax请求并且会话结束,我将打开一个登录对话框,否则如果是正常请求,我将重定向到登录页面。你感兴趣吗?如果你不想打开对话框,在得到你想要的ajax响应之前不要调用代码来打开对话框。嗨,RossiDead,我该怎么做?我现在在上面添加了jquery模式代码。在运行此程序之前,我如何检查此程序?抱歉,我的jquery不太好。谢谢Olivier,我会尝试一下并让你知道!谢谢Oliver,这个答案看起来不错,但对我来说不起作用,我最终还是和我的会话超时处理程序呆在一起。。。
$(document).ajaxError(function (xhr, props) {
    if (props.status == 403 ) {
        ////session redirect goes here 
        var pathArray = window.location.pathname.split('/');
        var segment_1 = pathArray[1];
        var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/SessionTimeout";
        window.location = newURL;
    }
});