Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

C#
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 :
C#
<?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.
Posted
Comments
Dholakiya Ankit 7-Aug-13 1:19am    
which line you are getting error?

1 solution

set
XML
<pages enableSessionState="true" />

in your web.config


Thanks
Bhupendra
 
Share this answer
 
Comments
adriancs 6-Aug-13 13:03pm    
He had already added.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900