Best way to modify session timeout for some pages
For you what is the best way to do that? I would like that all the pages of my app have timeout of 20 开发者_运维百科minutes but 4. I would like these 4 pages have 60 minutes timeout..
What is the best way?Have I to set the Session.timeout in the constructor of the pages? Is there any other way?
Thanks
You could create a custom base page (that inherits from System.Web.UI.Page), set the session timeout in the Page_Init handler, and then set those four pages to inherit from your custom base page. That gives you one place to manage the session timeout value.
You can configure this through web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<sessionState timeout="20"/>
<location path="Page1.aspx">
<system.web>
<sessionState timeout="60"/>
</system.web>
</location>
<location path="Page2.aspx">
<system.web>
<sessionState timeout="60"/>
</system.web>
</location>
<location path="Page3.aspx">
<system.web>
<sessionState timeout="60"/>
</system.web>
</location>
</configuration>
Be sure to set the idle timeout for the application pool as well:
http://weblogs.asp.net/aghausman/archive/2009/02/20/prevent-request-timeout-in-asp-net.aspx
According to the docs, you can set the timeout at anytime. Of course it has to be while during a request to the server! :-)
You could implement a custom Page attribute, like:
using System;
System.Web.SessionState;
public class TimeoutControlPage: System.Web.UI.Page {
public int Timeout {
get { return Session.Timeout; }
set { Session.Timeout = value; }
}
}
and then save the below page as "test.aspx":
<%@ Page Language="C#" Timeout="60" Inherits="TimeoutControlPage" %>
<html>
<body>
</body>
</html>
精彩评论