【发布时间】:2017-11-07 01:22:59
【问题描述】:
我将我的 ASP.Net 应用程序上传到共享主机(godaddy plesk windows 服务器)。共享主机上的空闲超时设置为 5 分钟且无法更改。
我听说可以在超时期限过去之前通过 ping 服务器来重置会话计时器,我正在尝试这样做,但我的代码似乎不起作用。我以此为指导:
Keeping ASP.NET Session Open / Alive(玛丽安的帖子) 和
https://gist.github.com/KyleMit/aa4f576fa32bf36fbedab5540c18211d
基本上在我的 Home 控制器中,我已经输入了以下代码:
[HttpPost]
public JsonResult KeepSessionAlive()
{
return new JsonResult {Data = "Success"};
}
在我的 Scripts 文件夹中,我将这个脚本命名为 SessionUpdater.js
SessionUpdater = (function () {
var clientMovedSinceLastTimeout = true; //I want it to be always on so if statement fires
var keepSessionAliveUrl = null;
var timeout = 1 * 1000 * 60; // 1 minutes
function setupSessionUpdater(actionUrl) {
// store local value
keepSessionAliveUrl = actionUrl;
// setup handlers
listenForChanges();
// start timeout - it'll run after n minutes
checkToKeepSessionAlive();
}
function listenForChanges() {
$("body").one("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 == true) {
$.ajax({
type: "POST",
url: keepSessionAliveUrl,
success: function (data) {
// reset movement flag
clientMovedSinceLastTimeout = true; //always on
// start listening for changes again
listenForChanges();
// restart timeout to check again in n minutes
checkToKeepSessionAlive();
},
error: function (data) {
console.log("Error posting to " & keepSessionAliveUrl);
}
});
}
}
// export setup method
return {
Setup: setupSessionUpdater
};
})();
然后在我的布局页面和我的主页\索引页面中(以防部分视图由于某种原因不起作用)我把这个参考:
<script type="text/javascript">
// initialize Session Updater on page
SetupSessionUpdater('/Home/KeepSessionAlive');
SessionUpdater.Setup('@Url.Action("KeepSessionAlive","Home")');
</script>
我的问题是,尽管这样做了,会话仍然每 5 分钟空闲一次就会让我退出,我可以说应用程序池会回收,因为网站需要永远重新启动。我究竟做错了什么?我怎样才能让这个停止让我退出。
感谢任何信息或指出正确的方向。
【问题讨论】:
-
用这个改变你的web.config,
-
很遗憾,这没有帮助。
标签: jquery asp.net session iis hosting