Microsoft KB Archive/837376

= Session state cannot be used in ASP.NET with Windows SharePoint Services =

Article ID: 837376

Article Last Modified on 9/26/2007

-

APPLIES TO


 * Microsoft Windows SharePoint Services
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition

-



SYMPTOMS
When you have Microsoft Windows SharePoint Services (WSS) installed on your computer and when you browse the page that is set with session variables in the Microsoft ASP.NET Web application, you may 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



CAUSE
This problem may occur after you install WSS on a server that has Microsoft Visual Studio .NET 2003 installed. The WSS ISAPI filter handles all incoming URLs. When you browse one of the ASP.NET Web application virtual directories, the ISAPI filter does not locate the URL path of the folder.

For example, this problem may occur when you visit a URL that is similar to the following:

http:// / /WebForm1.aspx

In this example, the folder that is named  does not exist in the WSS database, and you cannot browse the folder.



WORKAROUND
To work around this problem, follow these steps.

Modify the Web.config file
Make the following modifications in the Web.config file of the Web application:
 * Clear the WSS ASP.NET handler, and then specify the default ASP.NET handler for all pages.
 * Set the trust level to Full to allow the ASP.NET Web application to run correctly.
 * Enable the session module.
 * Enable Session state for all the pages that are in the Web application.

Create an ASP.NET Web application
 Start Microsoft Visual Studio .NET 2003. On the File menu, point to New, and then click Project. The New Project dialog box appears. Under Project Types, click Visual Basic Projects, under Templates, click ASP.NET Web Application, and then click OK. By default, the WebForm1.aspx Web form is created. In Solution Explorer, right-click WebForm1.aspx, and then click View Code.  Replace the existing code in the WebForm1.aspx.vb file with the following code: Public Class WebForm1 Inherits System.Web.UI.Page


 * 1) Region &quot; Web Form Designer Generated Code &quot;

'The Web Form Designer requires this call.  Private Sub InitializeComponent

End Sub Protected WithEvents Button1 As System.Web.UI.WebControls.Button

'NOTE: The Web Form Designer requires the following placeholder declaration. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Init 'CODEGEN: The Web Form Designer requires this method call. 'Do not modify it by using the Code editor. InitializeComponent End Sub


 * 1) End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load 'Put user code to initialize the page here. Session.Add(&quot;Test&quot;, 1) Session.Add(&quot;Test1&quot;, 2) Session.Add(&quot;Test2&quot;, 3) End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click Response.Redirect(&quot;WebForm2.aspx&quot;) End Sub End Class  Switch to the Design view of the WebForm1.aspx Web form.</li> On the View menu, click HTML Source.</li>  Replace the existing HTML source code for the WebForm1.aspx Web form with the following HTML code: <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm1.aspx.vb&quot; Inherits=&quot;WebApplication5.WebForm1&quot;%> <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> WebForm1 <meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio .NET 7.1&quot;> <meta name=&quot;CODE_LANGUAGE&quot; content=&quot;Visual Basic .NET 7.1&quot;> <meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;> <meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;> </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <asp:Button id=&quot;Button1&quot; style=&quot;Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px&quot; runat=&quot;server&quot; Text=&quot;Button&quot;> </asp:Button> </HTML> </li> Add another Web form to the existing project. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> On the File menu, click Add New Item.The Add New Item dialog box appears.</li> In the Categories box, click Web Project Items, and then double-click Web Form in the Templates box. By default, a Web form that is named WebForm2.aspx is created.</li></ol> </li>  Replace the existing code in the WebForm2.aspx.vb file with the following code: Public Class WebForm2 Inherits System.Web.UI.Page


 * 1) Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent

End Sub

'NOTE: The Web Form Designer requires the following placeholder declaration. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Init 'CODEGEN: The Web Form Designer requires this method call. 'Do not modify it by using the Code editor. InitializeComponent End Sub


 * 1) End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load 'Put user code to initialize the page here. Response.Write(Session(&quot;Test&quot;) & &quot; &quot;) Response.Write(Session(&quot;Test1&quot;) & &quot; &quot;) Response.Write(Session(&quot;Test2&quot;)) End Sub

End Class </li>  Add the following directives in the Web.config file of the Web application between the <system.web> tag and the </system.web> tag:

<httpHandlers> <add verb=&quot;*&quot; path=&quot;*.aspx&quot; type=&quot;System.Web.UI.PageHandlerFactory, System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&quot; /> </httpHandlers>

<trust level=&quot;Full&quot; originUrl=&quot;&quot; />

<httpModules> <add name=&quot;Session&quot; type=&quot;System.Web.SessionState.SessionStateModule&quot;/> </httpModules>

<pages enableSessionState=&quot;true&quot; enableViewState=&quot;true&quot; enableViewStateMac=&quot;true&quot; validateRequest=&quot;false&quot; /> </li> Save the modified Web.config file.</li> Exclude the  folder by using SharePoint Central Administration. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> On the server that is running WSS, click Start, click Administrative Tools, and then click SharePoint Central Administration.</li> In the Virtual Server Configuration area, click Configure virtual server settings.</li> In the Virtual Server List list, click the virtual server that you must add the excluded paths to.</li> Under Virtual Server Management, click Define managed paths.</li> In the Add a New Path section, type the path that you want to exclude in the Path box.</li> Click Excluded Path, and then click OK.</li></ol> </li></ol>

After you set these settings in the Web.config file of your Web application, you can browse the Web application, and then use the sessions in your Web pages.

<div class="status_section">

STATUS
This behavior is by design.

<div class="references_section">