2017-04-08 71 views
0

我想找到一种方法来保持我的会话活着,当控制器需要很长时间才能返回结果。我的Javascript按钮点击如下所示:ASP.net MVC保持会话活跃

function OnClick(s, e) { 
     positionDate = ReportingPositionDate.GetDate().toDateString(); 

     if (true) { 
      $.ajax({ 
       type: "POST", 

       url: "@Url.Action("DataFileUpload", "ImportData")", 
       data: JSON.stringify({ positionDate: positionDate }), 
       dataType: "text", 
       contentType: "application/json; charset=utf-8", 
       beforeSend: function() { lpImport.Show(); }, 
       success: function (msg) { 
        debugger; 
        ImportDataGridView.PerformCallback(); 
        ImportSuccessMessage.SetVisible(true); 
        ImportSuccessMessage.SetText(msg); 
        lpImport.Hide(); 
       }, 
       Error: function (xhr) { 
        alert(xhr) 
        ImportDataGridView.PerformCallback(); 
       } 
      }); 
     } 
    } 

基本上会话超时我成功之前。我想默默地保持会话活着。 谢谢大家。

回答

0

我在网上搜索了相同的内容。有回应,但他们不正确。 我最终能够调整其中一个,这是结果:

- 创建一个asp.net mvc应用程序; 添加以下到主控制器:

[HttpPost] 
    public JsonResult KeepSessionAlive() 
    { 

     return new JsonResult { Data = "Postback " + on " + DateTime.Now}; 
    } 

----添加以下index.cshtml:

<div id="myDiv"></div> 
@section scripts{ 
<script src="~/SessionUpdater.js"></script> 
<script type="text/javascript"> 
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")'); 
</script> 

} - 参考下面的js文件[除了引用的jQuery ]

SessionUpdater = (function() { 
var clientMovedSinceLastTimeout = false; 
var keepSessionAliveUrl = null; 
//var timeout = 5 * 1000 * 60; // 5 minutes 
var timeout = 15000; // 15 seconds for testing 

function setupSessionUpdater(actionUrl) { 
    // store local value 
    keepSessionAliveUrl = actionUrl; 
// alert(actionUrl); 
    // setup handlers 
    listenForChanges(); 
    // start timeout - it'll run after n minutes 
    checkToKeepSessionAlive(); 
} 

function listenForChanges() { 
    $("body").on("mousemove keydown", function() { 
     clientMovedSinceLastTimeout = true; 
    }); 
} 


// fires every n minutes - if there's been movement ping server and restart timer 
function checkToKeepSessionAlive() { 
    setTimeout(function() { keepSessionAlive(); }, timeout); 
} 

function keepSessionAlive() { 
    // if we've had any movement since last run, ping the server 


    if (!clientMovedSinceLastTimeout && keepSessionAliveUrl != null) { 
     $.ajax({ 
      type: "POST", 
      url: keepSessionAliveUrl, 
      success: function (data) { 
       $('#span').text(data); 
       $('#myDiv').append('<br/>' + data); 
       // reset movement flag 
       clientMovedSinceLastTimeout = false; 
       // start listening for changes again 
       listenForChanges(); 
       // restart timeout to check again in n minutes 
       checkToKeepSessionAlive(); 
      }, 
      error: function (data) { 
       alert("ERROR"); 
       console.log("Error posting to " & keepSessionAliveUrl); 
      } 
     }); 
    } 
    else { 
     clientMovedSinceLastTimeout = false; 
     listenForChanges(); 
     checkToKeepSessionAlive(); 
    } 

} 

// export setup method 
return { 
    Setup: setupSessionUpdater 
}; 

})();