会话结束时调用操作方法

时间:2017-01-31 09:43:18

标签: javascript c# asp.net-mvc session actionmethod

我想调用LogOut操作方法,当会话超时时,View i尚未创建。

我已经为会话超时编写了一个脚本,但我不知道如何调用action方法,因为我所拥有的所有方法(如window.location等)都会定位视图。

    <script>
            //session end

        var sessionTimeoutWarning = @Session.Timeout;

        var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000-55000;
        setTimeout('SessionEnd()', sTimeout);

    /* Here I want To call that AgentLogOut Method*/

        function SessionEnd() {
            alert("Session Is Going To End in 1 min Please Login Again1");

            window.location = "/Agent/AgentLogIn";
        }
</script>

这就是我想要调用的目标控制器动作

public ActionResult AgentLogOut()
        {
            string  SessionId = Session["LogInSession"].ToString();
            string OType = "LogOut";
            ProcedureName = "SP_Crud";
            XElement xl = new XElement("data",
                new XAttribute("otype", OType),
                new XElement("sessionId", SessionId),
                new XElement("agentIp", AgentIp)
                );
            objDal.ExecuteNonQuery(ProcedureName, CommandType.StoredProcedure, new MySqlParameter("@xml", xl.ToString()));
            Session.Clear();
            Session.Abandon();

            return RedirectToAction("AgentLogIn","Agent");
        }

我已经尝试过所有我认识的方法。请告诉我如何只使用动作方法。

2 个答案:

答案 0 :(得分:2)

然后你可以发一个像ajax的请求:

function SessionEnd() 
{
    $.ajax({
    type: "post",
    url: "/Agent/AgentLogIn",
    data:{data:value},
    success:function(response){
    //do some stuff like login page redirection
    },
    error:function(){
    //do some stuff like login page redirection
    }
  });
}

答案 1 :(得分:1)

  var sessionTimeoutWarning = @Session.Timeout;

    var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000-55000;
    setTimeout(function SessionEnd() {
        alert("Session Is Going To End in 1 min Please Login Again1");
       window.location = "/Agent/AgentLogIn";
    }, sTimeout);

OR

    var sessionTimeoutWarning = @Session.Timeout;

    var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000-55000;
    setTimeout(SessionEnd, sTimeout);



    function SessionEnd() {
        alert("Session Is Going To End in 1 min Please Login Again1");
        window.location = "/Agent/AgentLogIn";
    }

注意确保:您已将Session.Timeout设置为整数值