简体   繁体   English

会话丢失时重定向页面?

[英]Re-direct page when session is lost?

I am using ASP.NET. 我正在使用ASP.NET。

If my sessions time out i want to re-direct the page to another URL: Say the home page.... 如果我的会话超时,我想将页面重定向到另一个URL:说主页...。

On my page I make use of a GridView that uses Session variables. 在我的页面上,我使用了使用Session变量的GridView。 If the session variable time expire(currently at 60min) and the user click on a row in the GridView I want to re-direct him/her to the home page of my site. 如果会话变量时间到期(当前为60分钟),并且用户在GridView中单击一行,我想将他/她重定向到我网站的主页。 Can this be done, how would I do this? 能做到这一点,我该怎么做?

Thanks in advance! 提前致谢!

Store some value in Session collection. 在会话集合中存储一些值。 Then check if it's still there at the next user's request. 然后检查下一个用户的请求是否仍然存在。 If not, redirect. 如果不是,请重定向。

// Put some session marker //放置一些会话标记

Session["IsOldSession"] = true;

// Then later... //然后……

if (Session["IsOldSession"] == null)
    Response.Redirect ("~/OMG.aspx");

Alternatively you may simply rely on the the count of items in the session collection via using Session.Contents.Count . 另外,您可以通过使用Session.Contents.Count简单地依赖会话集合中项目的计数。 I would prefer this route over instantiating an additional item to persist in the session state collection. 与实例化其他项以保留在会话状态集合中相比,我更希望使用此路由。

If Session.Contents.Count = 0 Then
     Response.Redirect("~/default.aspx")
End If

Or just do this in your Page_Init() 或者只是在您的Page_Init()中执行此操作

if (Session.IsNewSession)
{
    Response.Redirect("homepage.aspx");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM