Microsoft KB Archive/306996

= How To Disable ASP Session State in ASP.NET =

Article ID: 306996

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q306996



For additional information about how to perform this task by using Microsoft Active Server Pages, click the article number below to view the article in the Microsoft Knowledge Base:

244465 How To Turn Off ASP Session State in Active Server Pages and IIS

IN THIS TASK
SUMMARY
 * Disable Session State at the Application Level
 * Disable Session State at the Page Level
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article demonstrates how to disable session state in ASP.NET.

When session state is enabled, ASP.NET creates a session for every user who accesses the application, which is used to identify the user across pages within the application. When session state is disabled, user data is not tracked, and you cannot store information in the Session object or use the Session_OnStart or Session_OnEnd events. By disabling session state, you can increase performance if the application or the page does not require session state to enable it.

In ASP.NET, if you do not use the Session object to store any data or if any of the Session events (Session_OnStart or Session_OnEnd) is handled, session state is disabled. A new Session.SessionID is created every time a single page is refreshed in one browser session.

back to the top

Disable Session State at the Application Level
The following steps demonstrate how to disable session state at the Application level, which affects all pages in the application:
 * 1) Start Microsoft Visual Studio .NET, and create a new ASP.NET Web Application.
 * 2) In Solution Explorer, double-click Web.config to view the contents of this file.
 * 3) Locate the  section, and set the mode value to Off.
 * 4) Save the file and/or the project to disable session state throughout all pages in the application.

back to the top

Disable Session State at the Page Level
The following steps demonstrate how to disable session state at the Page level, which affects only the specific pages that enable these changes:  Start Microsoft Visual Studio .NET, and create a new ASP.NET Web Application. In Solution Explorer, double-click the Web Form for which you want to disable session state. Click the HTML tab.  At the top of the page, add EnableSessionState=&quot;false&quot; in the @ Page directive. The modified attribute should appear similar to the following: <%@ Page language=&quot;c#&quot; Codebehind=&quot;WebForm1.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;WebApplication1.WebForm1&quot; EnableSessionState=&quot;false&quot; %>  Save the file and/or project to disable session state throughout all pages in the application.

back to the top

Troubleshooting
If you try to set or retrieve information when session state is disabled, you receive the following error message:

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive

back to the top

<div class="references_section">