ASP. NET status management-Session

Source: Internet
Author: User
Document directory
  • Simple Example

In the early days of Web development, Web pages were independent of each other. They seldom need to know the content of other pages (if you need to know it, it is generally transmitted by querying strings or using Form ). With the development of the Internet, Web pages become more complex. It is far from enough to simply browse a page and view its content. The current Web site design all wants to consider the workflow problem-that is, you need to move from one page to another.

Therefore, Session State emerged. The session status variable allows the Web page to save information related to a specific "session" of the user's browser. In the same session, the following pages can reference these session variables as needed. This information is bound to the user's browser session. This information is deleted once the browser session is closed. This information is stored on the Web server and is saved in all calls of the client browser.

ASP. NET can use it to create a unique SessionID (SessionID is unique to the client browser on the server, each client browser has a corresponding SessionID) to retrieve session information (if the browser supports cookies and ASP.. NET Web page configured to use Cookie), or by ASP. NET append to an encrypted string in the URL to retrieve session information (for browsers that do not support cookies or ASP.. NET Web page is configured to not use cookies ).

Session configuration in web. config:

<sessionState     mode="[Off|InProc|StateServer|SQLServer|Custom]"    timeout="number of minutes"    cookieName="session identifier cookie name"    cookieless=         "[true|false|AutoDetect|UseCookies|UseUri|UseDeviceProfile]"    regenerateExpiredSessionId="[True|False]"    sqlConnectionString="sql connection string"    sqlCommandTimeout="number of seconds"    allowCustomSqlDatabase="[True|False]"    useHostingIdentity="[True|False]"    stateConnectionString="tcpip=server:port"    stateNetworkTimeout="number of seconds"    customProvider="custom provider name">    <providers>...</providers></sessionState>
    
   

Session storage mode: InProc, StateServer, SQL Server, Custom, Off. The default storage mode is InProc.

InProc ModeIn this mode, the session status is stored in the memory of the Web server, and the session Status values and variables are stored in the memory of the Local Web server.

StateServer modeStores the session status in a separate process named ASP. NET status service. This ensures that the session status is retained when the Web application is restarted and the session status can be used on multiple Web servers in the network farm. To use this mode, set ASP. NET status to startup. ASP. NET state service processes are independent of ASP. NET auxiliary processes or IIS application pools. This mode ensures that the session status is retained when the Web application is restarted and that the session status can be used on multiple Web servers in the network farm. If you set StateServer mode, you must set the stateConnectionString attribute. For example:

<configuration>  <system.web>    <sessionState mode="StateServer"      stateConnectionString="tcpip=127.0.0.1:42424"      cookieless="false"      timeout="20"/>  </system.web></configuration>

SQLServer ModeStore the session status in an SQL Server database. This ensures that the session status is retained when the Web application is restarted and the session status can be used on multiple Web servers in the network farm. To use SQLServer mode, you must first ensure that the ASP. NET session status database is installed on SQL Server.

<configuration>  <system.web>    <sessionState mode="SQLServer"      sqlConnectionString="Integrated Security=SSPI;data         source=ASPNETServer;" />  </system.web></configuration>
     
     Custom ModeThis mode allows you to specify a custom storage provider.

Off ModeThis Mode disables the session status.

 

In ASP. NET, Session is a method and attribute.. NET object. NET session Status objects store any data types (including user-defined classes and structures ). However, data is stored at the performance cost. The cost depends on how much session information is saved or the type of data.

 

Simple Example

Create two pages: one page (SessionAdd. aspx) to add the Session Key and Value, and the other (SessionResult. aspx) to display the Session information.

SessionAdd code:

    <div>       Session Variable:<asp:TextBox ID="txtSessionVariable" runat="server" /><br/>       Value: <asp:TextBox ID="txtSessionValue" runat="server" /><br />             <asp:Button ID="btnAdd" runat="server" Text="Add Session"             onclick="btnAdd_Click" />      <asp:Button ID="banView" runat="server" Text="View Session"             onclick="banView_Click" />          </div>
 
Background code:
    protected void btnAdd_Click(object sender, EventArgs e)   {        Session[txtSessionVariable.Text] = txtSessionValue.Text;    }    protected void banView_Click(object sender, EventArgs e)    {        Server.Transfer("SessionResult.aspx");    }
Page effect:

   
 
SessionResult. aspz page:
    <div>      <asp:TextBox ID="txtSessionResult" runat="server" TextMode="MultiLine" Width="240px" Height="200px" /><br />      <asp:Button ID="btnBack" runat="server" Text="Clear and Back"             onclick="btnBack_Click" />    </div>
 
Background code:
   protected void Page_Load(object sender, EventArgs e)   {       if (!IsPostBack)       {           StringBuilder sb = new StringBuilder();           foreach (String strKey in Session.Keys)           {               sb.Append(strKey);               sb.Append("=");               sb.Append(Session[strKey]);               sb.Append("\n");           }           txtSessionResult.Text = sb.ToString();       }   }   protected void btnBack_Click(object sender, EventArgs e)   {       Session.Clear();       Server.Transfer("SessionAdd.aspx");   }
Page effect:

     
 
The preceding columns briefly describe how to add, view, and clear sessions.
Based on performance considerations, sessions should not store any content dependent on external links or key resources.
    
    
    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.