只有在enableSessionState设置为true时才能使用会话状态 [英] Session state can only be used when enableSessionState is set to true

查看:111
本文介绍了只有在enableSessionState设置为true时才能使用会话状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



当我使用HTTP模块进行URL重写时,我收到此错误:



只有在配置文件或Page指令中将enableSessionState设置为true时,才能使用会话状态。另请确保System.Web.SessionStateModule或自定义会话状态模块包含在< configuration> \< system.web> \< httpmodules>中。应用程序配置中的部分。



我的URL重写代码:



hello

when i use HTTP module for URL rewriting , i get this error:

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 <configuration>\<system.web>\<httpmodules> section in the application configuration.

My URL rewriting code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for URLRewriter
/// </summary>
public class URLRewriter : IHttpModule
{

    #region IHttpModule Members

    /// <summary>
    /// Dispose method for the class
    /// If you have any unmanaged resources to be disposed
    /// free them or release them in this method
    /// </summary>
    public void Dispose()
    {
        //not implementing this method
        //for this example
    }

    /// <summary>
    /// Initialization of the http application instance
    /// </summary>
    /// <param name="context"></param>
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }
    /// <summary>
    /// Event handler of instance begin request
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void context_BeginRequest(object sender, EventArgs e)
    {
        //Create an instance of the application that has raised the event
        HttpApplication httpApplication = sender as HttpApplication;

        //Safety check for the variable httpApplication if it is not null
        if (httpApplication != null)
        {
            //get the request path - request path is    something you get in
            //the url
            string requestPath = httpApplication.Context.Request.Path;

            //variable for translation path
            string translationPath = "";

            //if the request path is /urlrewritetestingapp/laptops/dell/
            //it means the site is for DLL
            //else if "/urlrewritetestingapp/laptops/hp/"
            //it means the site is for HP
            //else it is the default path
            switch (requestPath.ToLower())
            {
                case "/laptops/dell/":
                    translationPath = "/showitem.aspx?itemid=7";
                    break;
                case "/laptops/hp/":
                    translationPath = "/showitem.aspx?itemid=8";
                    break;
                default:
                    translationPath = requestPath.ToLower();
                    break;
            }

            //use server transfer to transfer the request to the actual translated path
            httpApplication.Context.Server.Transfer(translationPath);
        }
    }

    #endregion
}







我的配置代码:




My Config Code :

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  
  <system.webServer>
    <modules>
      <add name="URLRewriter" type="URLRewriter"/>
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
  <connectionStrings>
    <add name="conn" connectionString="Data Source=.;Initial Catalog=CMS;User ID=sa;Password=123456" />
  </connectionStrings>
  <system.web>




      <compilation debug="true" targetFramework="4.0">

          <assemblies>
          <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
          <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        </assemblies>
      </compilation>

      <httpRuntime targetFramework="4.0"/>
    <pages enableSessionState="true" />
    <sessionState mode="StateServer"></sessionState>
      <httpModules >
        <add name="URLRewriter" type="URLRewriter"/>
        <add type="System.Web.SessionState.SessionStateModule" name="Session" />

      </httpModules>
  </system.web>
 
  <appSettings>
    <add key="webpages:Enabled" value="true"/>
  </appSettings>

 
</configuration>







请帮助我。谢谢。




Please Help Me . Thank You.

推荐答案

set
<pages enableSessionState="true" />

in your web.config





谢谢

Bhupendra



Thanks
Bhupendra


这篇关于只有在enableSessionState设置为true时才能使用会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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