2013-10-22 14 views
0

無法使用會話當我嘗試使用一個會話veriable在InitializeCulture,我得到了如下因素的錯誤:在InitializeCulture

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the \\ section in the application configuration.

這就是引發錯誤的代碼

public class BasePage : Page 
{ 
    public string Lang { get; set; } 

    protected override void InitializeCulture() 
    { 
     if (!Page.IsPostBack) 
     { 
      if (!string.IsNullOrEmpty(Request.QueryString["lang"])) 
      { 

       Session["lang"] = Request.QueryString["lang"].ToLower(); 
      } 


      if (Session["lang"] != null && !string.IsNullOrEmpty((string)Session["lang"])) 
      { 
       Lang = (string)Session["lang"]; 
       string selectedCulture = Lang == "nl" ? "nl-BE" : "fr-BE"; 

       UICulture = selectedCulture; 
       Culture = selectedCulture; 

       Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedCulture); 
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedCulture); 
      } 

      base.InitializeCulture(); 
     } 
    } 
} 

我改變了我的Web.Config,就像在錯誤中獲得的一樣。但它一直在拋出同樣的錯誤。

<pages enableSessionState="true"> 
       <controls> 
       <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
       <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
      </controls> 
     </pages> 
     <httpModules> 
      <remove name="Session" /> 
      <add name="Session" type="System.Web.SessionStateModule" /> 
      <add type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" name="UrlRewriter"/> 
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     </httpModules> 

有沒有人有解決方案?謝謝!!!

回答

1

嘗試在pages部分設置enableSessionState = true。

<system.web> 
    <pages enableSessionState="true"> 
</system.web> 

此外,如果您是在Windows 2008中使用IIS7,你的模塊應該是<system.webServer>段內,而不是<system.web>

+0

感謝您的回覆。不幸的是,這是行不通的。 – Vinzcent

+0

您是否檢查過ASP.NET狀態服務是否正在運行? – rla4

+1

此外,如果您在Windows 2008上使用IIS7,則您的模塊應位於部分中,而不是 - http://stackoverflow.com/questions/2757403/error-using-session-in- iis-7 – rla4

相關問題