在我的一个 Web 应用程序中,我需要在 5 分钟内会话超时时弹出警报。用户可以选择继续延长会话或立即注销。
在 Web.config 中将会话超时设置为 30 分钟:
<sessionState mode="InProc" timeout="30">
Run Code Online (Sandbox Code Playgroud)
由于 ASP.NET MVC 没有提供检查剩余会话超时的方法,我提出了如下解决方案:
在 Global.asax 中,它将当前会话的上次访问时间作为会话变量进行跟踪。如果传入请求不是只读会话状态(见下文),则上次访问时间会话变量将更新为当前时间。否则,会话变量的值将设置为当前时间。
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
var context = HttpContext.Current;
if (context != null && context.Session != null && !context.Session.IsReadOnly)
{
context.Session["_LastAccessTime"] = DateTime.Now;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的会话控制器中,我将会话状态行为设置为只读。对此控制器的请求既不会重置会话,也不会刷新我的上次访问时间会话变量。
[SessionState(SessionStateBehavior.ReadOnly)]
public class SessionController : BaseController
{
[AjaxOnly]
public ActionResult GetRemainingSessionTimeout()
{
if (!Request.IsAjaxRequest())
{
return Content("Not an Ajax call.");
}
var remainingSessionTimeout = 0;
if (Session["_LastAccessTime"] != null)
{ …
Run Code Online (Sandbox Code Playgroud) 对象通过引用传递。它们永远不会被复制。我有如下代码段:
var person={firstname:'John', lastname:'Smith'}
var anotherPerson=person
anotherPerson.nickname='Curly'
console.log(person.nickname)
"Curly"
var fname=person.firstname
console.log(fname)
"John"
person.firstname = 'Tom'
console.log(anotherPerson)
Object {firstname: "Tom", lastname: "Smith", nickname: "Curly"}
console.log(fname)
"John" <-- fname is not updated
Run Code Online (Sandbox Code Playgroud)
我的问题是在我将对象人的名字更新为“Tom”之后,为什么本地变量 fname 没有更新?