无法在InitializeCulture中使用会话 [英] Can't use Session in InitializeCulture

查看:63
本文介绍了无法在InitializeCulture中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在InitializeCulture中使用Veriable会话时,出现以下错误:

When I try to use a Session veriable in InitializeCulture, I get the folowing error:

只有在配置文件或Page指令中将enableSessionState设置为true时,才可以使用会话状态.还请确保在应用程序配置的\\部分中包含System.Web.SessionStateModule或自定义会话状态模块.

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.但是它总是抛出相同的错误.

I changed my Web.Config, as sugested in the error. But It keeps throwing the same error.

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

有人可以解决吗?谢谢!!!

Does anyone has a solution? Thanks!!!

推荐答案

尝试在页面部分中设置enableSessionState = true.

Try to set enableSessionState=true in the pages section.

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

此外,如果您在Windows 2008上使用IIS7,则模块应位于< system.webServer> 部分中,而不是< system.web> >.

Also, if you're using IIS7 on Windows 2008, your modules should be inside the <system.webServer> section, instead of <system.web>.

这篇关于无法在InitializeCulture中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆