Session state cannot be used in ASP.NET with Windows SharePoint Services (837376)



The information in this article applies to:

  • Microsoft Windows SharePoint Services
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • 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://ServerName/WebApplication/WebForm1.aspx

In this example, the folder that is named WebApplication 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

  1. Start Microsoft Visual Studio .NET 2003.
  2. On the File menu, point to New, and then click Project. The New Project dialog box appears.
  3. 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.
  4. In Solution Explorer, right-click WebForm1.aspx, and then click View Code.
  5. Replace the existing code in the WebForm1.aspx.vb file with the following code:
    Public Class WebForm1
        Inherits System.Web.UI.Page
    
    #Region " Web Form Designer Generated Code "
    
        'The Web Form Designer requires this call.
        <System.Diagnostics.DebuggerStepThrough()> 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
    
    #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("Test", 1)
            Session.Add("Test1", 2)
            Session.Add("Test2", 3)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
            Response.Redirect("WebForm2.aspx")
        End Sub
    End Class
    
  6. Switch to the Design view of the WebForm1.aspx Web form.
  7. On the View menu, click HTML Source.
  8. Replace the existing HTML source code for the WebForm1.aspx Web form with the following HTML code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" 
    Inherits="WebApplication5.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <title>WebForm1</title>
      <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
      <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <meta name="vs_targetSchema" 
    content="http://schemas.microsoft.com/intellisense/ie5">
     </HEAD>
      <body MS_POSITIONING="GridLayout">
       <form id="Form1" method="post" runat="server">
         <asp:Button id="Button1" style="Z-INDEX: 101; 
    LEFT: 8px; POSITION: absolute; TOP: 8px"  runat="server" Text="Button">
         </asp:Button>
      </form>
      </body>
    </HTML>
    
  9. Add another Web form to the existing project. To do this, follow these steps:
    1. On the File menu, click Add New Item.The Add New Item dialog box appears.
    2. 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.
  10. Replace the existing code in the WebForm2.aspx.vb file with the following code:
    Public Class WebForm2
        Inherits System.Web.UI.Page
    
    #Region " Web Form Designer Generated Code "
    
        '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
    
    #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("Test") & "<br>")
            Response.Write(Session("Test1") & "<br>")
            Response.Write(Session("Test2"))
          
        End Sub
    
    End Class
    
  11. Add the following directives in the Web.config file of the Web application between the <system.web> tag and the </system.web> tag:
    <!-- 
    Clear out the WSS ASP.NET handler and specify the default ASP.NET handler for all pages. 
    -->
    
    <httpHandlers>
    <clear />
    <add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory, System.Web, 
    Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </httpHandlers>
    
    <!-- 
    Set the trust to Full. WSS configures a very restrictive trust policy that does not 
    allow ASP.NET Web application to run correctly.
    -->
    
    <trust level="Full" originUrl="" />
    
    <!-- 
    Enable the session module. This can also be enabled on the WSS Web.config, 
    but is not enabled by default. If you receive the following message:
    Parser Error Message: The module 'Session' is already in the application 
    and cannot be added again. You can remove the following 
    <httpModules></httpModules> section as session is already enabled 
    on the virtual server. 
    --> 
    
    <httpModules>
    <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
    </httpModules> 
    
    <!-- Enable session state for all the pages in the Web application. --> 
    <pages enableSessionState="true" enableViewState="true" 
    enableViewStateMac="true" validateRequest="false" />
    
  12. Save the modified Web.config file.
  13. Exclude the WebApplication folder by using SharePoint Central Administration. To do this, follow these steps:
    1. On the server that is running WSS, click Start, click Administrative Tools, and then click SharePoint Central Administration.
    2. In the Virtual Server Configuration area, click Configure virtual server settings.
    3. In the Virtual Server List list, click the virtual server that you must add the excluded paths to.
    4. Under Virtual Server Management, click Define managed paths.
    5. In the Add a New Path section, type the path that you want to exclude in the Path box.
    6. Click Excluded Path, and then click OK.
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.

STATUS

This behavior is by design.

Modification Type:MinorLast Reviewed:8/24/2004
Keywords:kbsetup kbASPNET kbState kbConfig kbprb KB837376 kbAudDeveloper