Method 1: use the same method to access the session. In this way, you can determine and jump to the session.
Method 2: 1. Start the service "ASP. NET state service ",
2. Then, modify web. config:
<Sessionstate
Mode = "StateServer"
Stateconnectionstring = "TCPIP = 127.0.0.1: 42424"
Sqlconnectionstring = "datasource = 127.0.0.1; trusted_connection = yes"
Cookieless = "false"
Timeout = "140000"
/>
Note: // mode = "StateServer" In this mode, the session will not be lost even if the page is modified!
Timeout = "140000" can be set to a greater value here !!!
Of course:
Mode = "inproc" If your mode is set to this type, the session will be lost when you modify the page !!!!!!
Method 3: defined in Web. config:
<Customerrors defaultredirect = "errorpage. aspx "mode =" on "/> but the minor disadvantage of this method is: I don't know whether it is caused by session expiration or due to other errors on the page, and cannot jump on errorpage. given in Aspx.
Method 4: Create a. ascx file and write the verification code in it. You can just drag it over when using it.
Or process it in handler and module.
Void session_end (Object sender, eventargse)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session mode is set to StateServer
// Or sqlserver, the event is not triggered.
}
System. WebThe namespace provides classes and interfaces for communication between browsers and servers. This namespace includes the httprequest class that provides extensive information about the current HTTP request, the httpresponse class that manages the HTTP output to the client, and the httpserverutility class that provides access to server utilities and processes.System. WebIt also includes classes for Cookie operations, file transmission, exception information, and output Cache control.
System. Web. sessionstateThe namespace provides classes and interfaces for storing data specific to a single client in a web application on the server. Session status data is used to provide the client with a style of persistent connection with the application. The status information can be stored in the local process memory or stored outside the process by using ASP. Net status service or SQL Server database for network farm configuration. The session status can be used with clients that do not support cookies. ASP. NET can be configured to encode the session ID in the URL string transmitted between the client and the server.
System. Web. sessionstate. httpsessionstateThe session we use is actually the object of this class. The session is created in the first request after it is used.Session. isnewsession.Publicsealed class httpsessionstate: icollection, ienumerableFrom this statement, we can see that httpsessionstate inherits from the icollection interface, indicating that all future sessions ["XXX"] will be added to the session collection. We get a session by name and actually use the index.
Attribute:
- Iscookieless:Obtain a value indicating whether the session ID is embedded in a URL or stored in an HTTP cookie. If the session is embedded in the URLTrueOtherwiseFalse.
- Issynchronized: Identifies whether a session is synchronized (thread-safe). The default value of session. issynchronized is false because of the extremely serious multi-window operation session overwrites each other.
- Timeout: Set the Session Timeout time. In minutes.
System. Web. sessionstate also has an enumeration variable, system. Web. sessionstate. sessionstatemode, which indicates four statuses of the session:
- Inproc, which is executed together with the aspnet_wp process by default.
- Off. The session value is disabled.
- Sqlserver, the session status is stored by the SQL Server outside the process.
- StateServer: The session status is stored by the Windows NT Server outside the process.
The following are some questions about session extracted from the network:
- Q: Why didn't the session_end method be activated when session. Abandon is called?
A: The session_end method only supports sessions of the inproc (in-process) type. Second, to stimulate the session_end method, there must be a session (that is, the session has been used in the system), and at least one request must be completed (this method will be called in this request ).
- Q: Why is session frequently lost in inproc mode?
A: This problem is usually caused by application recycling. When an in-process session is used, the session is saved in the aspnet_wp process, when the process is recycled, the session will naturally disappear. To determine whether the process is recycled, you can obtain information by viewing the System Event Viewer.
For more information, see:
Session variables are lost intermittently in ASP. netapplications
Http://support.microsoft.com/default.aspx? SCID = KB; en-US; q2017148
At 1.0, a bug also causes the worker process to be recycled and restarted. The bug has been fixed in 1.1 and SP2.
For more information about this bug, see:
ASP. NET Worker Process (aspnet_wp.exe) is recycledunexpectedly.
Http://support.microsoft.com/default.aspx? SCID = KB; en-US; q321792
- Q: Why is the new session ID the same as the original one after the session times out or abandoned?
A: Because sessionid is saved in the client browser instance, when the session times out and the server re-creates the session, the session ID sent from the browser will be used. When the session times out, then, the sessionid remains unchanged.
- Q: What types of objects can be stored in the session?
A: This depends on the session mode. When using an inproc session, you can easily save any object. If you use the non-inproc mode, you can only save the objects that can be serialized and deserialized. If the stored objects do not support serialization, they cannot be saved to this mode (non-inproc).
- Q: Why can't I use the response. Redirect and server. transfer methods to jump to the page in session_end?
A: session_end is an event processing function that is triggered inside the server. It is based on the Internal timer of a server. When the event is triggered, there is no related httprequest object on the server. Therefore, the response. Redirect and server. transfer methods cannot be used at this time.
- Q: Can I obtain the httpcontext object in session_end?
A: No, because this event is not associated with any request, and there is no request-based context.
- Q: What is the difference between session. Abandon and session. Clear?
A: The main difference is that when session. Abandon is used, the session_end method is called (in inproc mode ). The session_start method is triggered when the next request arrives. Session. Clear only clears all data in the session and does not stop the session. Therefore, it does not call those methods.
- Q: Does the session provide a locking mechanism to allow sequential access to session Status values?
A: The session implements the reader/writer lock mechanism:
When the page has a writable function for the session (that is, the page has a <% @ pageenablesessionstate = "true" %> flag), the session of the page holds a write lock until the request completes.
When the page has the read-only function for the session (that is, the page is marked with <% @ pageenablesessionstate = "readonly" %>), the session of the page is read locked.
A read lock will block a write lock; A read lock will not block a read lock; A write lock will block all read/write locks. This is why when the same page in the two frameworks writes the same session, one of them starts to write after the other (the one that is a little faster) completes.
- Q: Why is the session lost when I set cookieless to true?
A: When cookieless is used, you must replace the absolute path in the program with the relative path. If ASP. NET is used, sessionid cannot be saved in the URL.
For example, replace/mydir/mysubdir/default. aspx with ../default. aspx.
- Q: How can I use session in my own class?
A: You can use httpcontext. Current. session as follows:
Httpcontext. Current. session ["sessionkey"] = "sessionvalue ";
Similarly, you can use the application object in this way.