Microsoft Knowledge Base Email Alertz

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

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 306996 - Last Review: May 16, 2007 - Revision: 3.6

How To Disable ASP Session State in ASP.NET

This article was previously published under Q306996

On This Page

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.

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 <sessionState> 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.

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:
  1. Start Microsoft Visual Studio .NET, and create a new ASP.NET Web Application.
  2. In Solution Explorer, double-click the Web Form for which you want to disable session state.
  3. Click the HTML tab.
  4. At the top of the page, add EnableSessionState="false" in the @ Page directive. The modified attribute should appear similar to the following:
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" 
    AutoEventWireup="false" Inherits="WebApplication1.WebForm1" 
    EnableSessionState="false" %>
    					
  5. Save the file and/or project to disable session state throughout all pages in the application.

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

REFERENCES

For more information about session state, refer to the following topics in the .NET Framework Software Development Kit (SDK) documentation:
ASP.NET Session State
http://msdn2.microsoft.com/en-us/library/ms972429.aspx (http://msdn2.microsoft.com/en-us/library/ms972429.aspx)

<sessionState> Section
http://msdn2.microsoft.com/en-us/library/h6bb9cz9(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/h6bb9cz9(vs.71).aspx)

APPLIES TO
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
Keywords: 
kbconfig kbhowtomaster kbstate kbwebforms KB306996
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate