Search code examples
javascriptc#asp.net-mvcsessionactionmethod

Call an action method when session ends


I want to call LogOut action method whose View i have not created as the session times out.

I have written a script for session time out but i dont know how to call action method because all the methods i have got like window.location etc locates the view.

    <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>

And this the Target Controller Action to which I want to call

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");
        }

I have tried all ways which i knew.Suggest me how to hit only the action method.


Solution

  • then you can make a ajax request like :

    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
        }
      });
    }